I have a table containing a list of folders, each folder has an ID and Description
SELECT * FROM folders
_id | description
---------------------------
1 | default
2 | test
I have an images table that contains a list of images, each image has a folderid
select folderid, count(*) as imagecount from images GROUP BY folderid;
folderid| imagecount
---------------------------
1 | 4
2 | 5
I want to be able to write a query that returns a list of all folders, with its description, and how many images is inside it
?????
_id | description | imagecount
------------------------------------------------------
1 | default | 4
2 | test | 5
I tried the following query:
SELECT _id, description from folders, (select folderid, c开发者_StackOverflowount(*) as imagecount from images GROUP BY folder) WHERE _id = folderid;
However it doesn't work, because it doesn't return the imagecount column. Any ideas?
This is also a valid SQLite query that will return the answer to your question: how many images in each folder?
select f.description, sum ( case when i.folderid is null then 0 else 1 end ) as imagecount
from folders f left join images i on i.folderid = f.id
group by f.description
or instead of the sum() with a case statement you could just do count(i.folderid) as imagecount
You're close...
SELECT _id, description, imagecount
from folders,
(select folderid, count(*) as imagecount from images GROUP BY folderid)
WHERE _id = folderid;
精彩评论