MySQL statement combining a join and a count?

前端 未结 4 1631
梦如初夏
梦如初夏 2020-12-08 01:40

I\'ve got a table of \'folders\'. I want to return all the records with the userId of 16.

SELECT * FROM `folders` WHERE userId = 16;

I\'ve

4条回答
  •  太阳男子
    2020-12-08 02:07

    Do a sub query that groups by the Folders to get the count per folder, then join it to the first query like this:

        select
           f.*
           fc.Files
        from
           Folders f
    --
           -- Join the sub query with the counts by folder     
           JOIN (select
                   Folder,
                   count(*) Files
                 from
                    files
                 group by
                    Folder ) as fc
              on ( fc.Folder = f.Folder )
        where
           f.userid = 16
    

提交回复
热议问题