Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Select Within a Select

Explorer ,
Dec 22, 2008 Dec 22, 2008
Hi,

I am using the code below to output all the names of my image galleries. Now I want to output the number of images in the galleries(my image table is called galleryImages). What is the best way achieve this a nested select?

Any help would be appreciated.
450
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Dec 22, 2008 Dec 22, 2008
Hulfy wrote:
> What is the best way achieve this a nested select?

Not a nested select. What you need is a JOIN between the two tables. A JOIN lets you retrieve information from both tables in the same query. You can then use the aggregate COUNT function to get the total number of matching records in your second table. If you are not familiar with JOINs, I would suggest reviewing a sql tutorial such as:

http://www.w3schools.com/sql/sql_join.asp
http://www.w3schools.com/sql/sql_func_count.asp
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 22, 2008 Dec 22, 2008
If you are able to JOIN the tables, then you can use the nest <cfoutput> tag
to get the required result.

--
Prasanth Kumar.S
"-==cfSearching==-" <webforumsuser@macromedia.com> wrote in message
news:gip1l4$o19$1@forums.macromedia.com...
> Hulfy wrote:
> > What is the best way achieve this a nested select?
>
> Not a nested select. What you need is a JOIN between the two tables. A
> JOIN
> lets you retrieve information from both tables in the same query. You can
> then
> use the aggregate COUNT function to get the total number of matching
> records in
> your second table. If you are not familiar with JOINs, I would suggest
> reviewing an sql tutorial such as:
>
> http://www.w3schools.com/sql/sql_join.asp
> http://www.w3schools.com/sql/sql_func_count.asp
>
>


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 23, 2008 Dec 23, 2008
<cfquery datasource="#application.datasource#" name="qGalleries">
SELECT *
FROM galleries
AS g
LEFT JOIN galleryImages
AS i
ON g.galleryID = i.imageGalleryID
ORDER BY galleryID
ASC
</cfquery>

Something like that. Then you might do:

<ul class="options">
<cfoutput query="qGalleries" group="galleryID">
<li><a href="editGallery.cfm?galleryId=#galleryId#">#galleryName#</a>
<ul>
<cfoutput group="imageID">
<li><a href="">#imageName#</a></li>
</cfoutput>
</ul>
</li>
</cfoutput>
</ul>

It's not exact, but is a rough example. It's not tested or anything.

Oh and, do you REALLY need to use * in your select? It gets all columns and rows. If you don't need all columns from both tables then define exactly what columns you need.

Mikey.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Dec 23, 2008 Dec 23, 2008
LATEST
Kapitaine wrote:
> <cfoutput query="qGalleries" group="galleryID">

If all they need is the total of the number of images, then a nested cfoutput is not necessary, just COUNT. But obviously that, and the type of JOIN, depends on the desired results. So it may be a good idea for them to review the sql tutorials to help them understand what sql is actually needed.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources