我有Mysql表包含字段(ip,postid)
喜欢:
ip | postid
x.x| 1
x.x| 2
x.x| 2
x.x| 3
x.x| 4
x.x| 2
x.x| 1
我希望按最大数量的postid排序我的查询结果而不重复
SELECT * FROM table order by count(postid) desc LIMIT 4
结果应该是,postid(2 | 1 | 3 | 4)
获得每个postid的计数,(postid 2 [3],postid 1 [2],等等)
答案 0 :(得分:1)
如果您想订购它,您必须使用group by和order by:
SELECT id,count(*) As `nr` FROM table GROUP BY postid ORDER BY nr DESC
答案 1 :(得分:1)
答案 2 :(得分:0)
使用GROUP BY
SELECT postid,count(postid) FROM table GROUP BY postid order by count(postid) desc LIMIT 4