开发者

MySQL Distinct Records With Count

开发者 https://www.devze.com 2023-01-05 01:00 出处:网络
I have a photos table, with two columns in that tab开发者_StackOverflowle named \"id\" and \"user_id\".Obviously one user can have many photos.I\'d like to run a query that can give me the photo count

I have a photos table, with two columns in that tab开发者_StackOverflowle named "id" and "user_id". Obviously one user can have many photos. I'd like to run a query that can give me the photo count for each user.

Any help is appreciated.


select user_id, count(*) as photo_count from photos group by user_id


Use:

SELECT t.user_id,
       COUNT(*) AS photo_count
  FROM PHOTOS
GROUP BY t.user_id


use count as your aggregate function and groupby clause to give the count of specific attribute,

here is the sample:

SELECT user_id, count(*) 'Photos Count'
from photos
group by user_id
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号