Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按最大数对查询结果排序,不重复_Php_Mysql - Fatal编程技术网

Php 按最大数对查询结果排序,不重复

Php 按最大数对查询结果排序,不重复,php,mysql,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 我想按最多的posted对查询结果进行排序,不要重复 SELECT * FROM table order by count(postid) desc LIMIT 4 结果应该是,postid(2 | 1 | 3 | 4) 要获得每个postid的计数,(postid 2[3],postid 1[2]等)使用GROUP

我有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
我想按最多的posted对查询结果进行排序,不要重复

SELECT * FROM table order by count(postid) desc LIMIT 4
结果应该是,postid(2 | 1 | 3 | 4)

要获得每个postid的计数,(postid 2[3],postid 1[2]等)

使用GROUP BY

SELECT postid,count(postid) FROM table GROUP BY  postid order by count(postid) desc LIMIT 4

当您希望订购时,必须使用group by和order by:

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

这就是你要找的吗

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

为什么这个答案被否决@你应该留下一个理由,也可以投反对票!!好吧,我想知道一个原因!!(+1)你不值得投反对票我想,downvoter是嫉妒的。@o6qr当你接受答案时,一定要接受在我之前给出的正确答案。因为之前的答案都是正确的,这对我来说是不公平的。@Abhik Chakraborty,太好了,这正是我想做的,谢谢你,兄弟:)@samitha |很好,但我也想按最多的数字排列结果
order by count(postid)ASC LIMIT 4
是的,[order by count(threadid)desc]现在很好用了。。在您修复代码之前,另一个人也给出了正确答案,我已经被选为他的答案,您是否原谅我?没有任何文档解释COUNT(*)的用法,这将无法解决此问题