SQL语句按最大条目数排序

问题描述

| 我有一个名为“报告”的表 看起来像:
user_id | report_post 
   1            2    
   2            2  
   3            2  
   4           10 
现在,我想首先列出前三个条目,因为该表中报告的帖子ID“ 2”是该表的3倍……我想按条目的最大值对它们进行排序。 希望你们能理解...非常感谢 - - 编辑 - - - 我的输出必须看起来像这样
report_post | entries
    2             3    
   10             1
    

解决方法

SELECT *
FROM (
    SELECT user_id,report_post,COUNT(*) AS cnt
    FROM reports
    GROUP BY report_post
) c
ORDER BY cnt DESC
    ,
Select report_post,Count(1) As Entries
From   reports
Group By Report_Post
Order By Count(1) DESC
    ,通过您的修改,这可以满足您的要求:
select report_post,count(*) entries
from reports
group by report_post
order by entries desc
    ,使用子查询。这应该可以解决MySQL中的问题:
select * from reports 
order by (
   select count(*) from reports as reports_a
   where reports_a.report_post=reports.report_post
) desc;
(以上答案是您的问题,您必须先对其进行编辑才能更改其含义) 对于已编辑的问题,这是一个琐碎的示例,其依据是:
select report_post,count(*) as entries
from reports
group by report_post
order by entries desc;
    ,
SELECT report_post as report_post,count(report_post) as entries 
FROM `reports` group by `report_post`
单一查询。     ,
SELECT * FROM reports ORDER BY entries DESC
    ,我不确定您到底要问什么。您是否只是想让它返回report_post id等于\“ 2 \”的条目? 如果是这种情况,这应该起作用:
SELECT * FROM reports WHERE report_post=2;
对不起,如果我误解了你的问题。