Skip to main content
Known Participant
June 23, 2011
Question

Item Count question

  • June 23, 2011
  • 1 reply
  • 430 views

I think this is more of a SQL query statement question rather than Coldfusion, but I'll attempt to explain what I'm trying to do.

I have two tables. One has a list of artwork and an artistID value is associated with each piece of artwork. That artistID value corresponds with a separate table that has artistID, artistFirst and artistLast values. On my CF page, I'm calling a random list of 20 records. My current SQL statement looks like this:

SELECT artgallery.imageID, artgallery.imageNum, artgallery.artistID, artists.artistID, artists.artistFirst, artists.artistLast

FROM artgallery, artists

WHERE artgallery.artistID = artists.artistID

ORDER BY RAND()

LIMIT 20

What I'm trying to figure out is how to display how many pieces of artwork an artists has in the database. I want it to display like this on the page:

Leonardo da Vinci - 20 works of art

The 20 would show that Leonardo da Vinci's artistID shows up 20 times in the main (artgallery) table. I'm stuck on figuring that one piece out.

This topic has been closed for replies.

1 reply

Inspiring
June 24, 2011

SELECT artgallery.imageID, artgallery.imageNum, artgallery.artistID, artists.artistID, artists.artistFirst, artists.artistLast

, pieces

FROM artgallery join artists on artgallery.artistID = artists.artistID

join (

select artistid, count(artistid) pieces

from artgallery

group by artistid

) temp on artists.artistid = temp.artistid

ORDER BY RAND()

LIMIT 20