表1 user
id username
1  sam
2  tins
3  stamins表2 comment
id comment author date
1  留言1   sam    2006-1-1
2  留言2   tins   2007-1-1
3  留言3   sam    2008-1-1
4  留言4   sam    2009-1-1请问怎么统计每个用户的留言数?就是查询结果username conmentcount
sam        3
tins       1
stamins    0

解决方案 »

  1.   

    SELECT COUNT(id), author FROM comment GROUP BY author
      

  2.   

    SELECT author,COUNT(id) FROM comment GROUP BY author
      

  3.   

    select count(c.id) as conmentcount ,u.username from comment c left join user u on c.author=u.username GROUP BY c.author
      

  4.   

    谢谢3楼的 我要的确是是这个 参考了jakey9826的发现自己少了给group by 结贴了