按最大数量对查询结果进行排序,不重复

时间:2014-05-03 11:26:34

标签: php mysql

我有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],等等)

3 个答案:

答案 0 :(得分:1)

如果您想订购它,您必须使用group by和order by:

SELECT id,count(*) As `nr` FROM table GROUP BY postid ORDER BY nr DESC

答案 1 :(得分:1)

这是你在找什么?

select 
postid, 
count(postid) as tot 
from test
group by postid
order by tot
desc

DEMO

答案 2 :(得分:0)

使用GROUP BY

SELECT postid,count(postid) FROM table GROUP BY  postid order by count(postid) desc LIMIT 4
相关问题