0

我有一张名为“报告”的表格

看起来像:

user_id | report_post 
   1            2    
   2            2  
   3            2  
   4           10 

现在我想首先列出前三个条目,因为报告的帖子 ID“2”在这个表中是 3 次......我想按条目的最大值对它们进行排序。

希望你们理解...非常感谢

----edit-------- 我的输出必须是这样的

report_post | entries
    2             3    
   10             1
4

7 回答 7

2
Select report_post, Count(1) As Entries
From   reports
Group By Report_Post
Order By Count(1) DESC
于 2011-05-19T23:05:54.903 回答
1

通过您的编辑,这可以满足您的要求:

select report_post, count(*) entries
from reports
group by report_post
order by entries desc
于 2011-05-19T23:08:18.430 回答
1
SELECT report_post as report_post, count(report_post) as entries 
FROM `reports` group by `report_post`

单查询。

于 2011-05-19T23:09:31.303 回答
1

使用子查询。这应该可以在 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;
于 2011-05-19T23:02:29.417 回答
1
SELECT *
FROM (
    SELECT user_id, report_post, COUNT(*) AS cnt
    FROM reports
    GROUP BY report_post
) c
ORDER BY cnt DESC
于 2011-05-19T23:02:45.400 回答
0
SELECT * FROM reports ORDER BY entries DESC
于 2011-05-19T23:03:30.347 回答
0

我有点不确定你到底在问什么。你只是想让它返回report_post id等于“2”的条目吗?

如果是这种情况,这应该有效:

SELECT * FROM reports WHERE report_post=2;

对不起,如果我误解了你的问题。

于 2011-05-19T23:04:09.743 回答