0

嗨,我有一个 Mysql 表,如下所示,

post_id  | user_id  |   status | date_created
  2          2         funny     20121022120627
  2          3         lame      20121023120627
  3          1         useful    20121023120627
  3          3         lame      20121023120627
  3          4         useful    20121023120627

现在我需要一个关于根据状态计数获取热门帖子的 post_id 的查询。表示当天有很多状态的帖子。示例post_id = 3会很受欢迎,因为它有 3 个状态投票,所以我可以在帖子表上找到这个帖子的所有者。谢谢!

4

2 回答 2

3

试试这个,

SELECT post_id
FROM tableName
GROUP BY post_ID
HAVING COUNT(*) = 
(
    SELECT MAX(x.counts)
    FROM
    (
        SELECT post_id, COUNT(*) counts
        FROM tableName
        GROUP BY post_id
    ) x
)

SQLFiddle 演示

基本上,我不建议在这里使用limit,因为某些人有可能Post_ID拥有相同的票数。

于 2012-10-23T04:31:55.720 回答
0
select t1.post_id,max(t1.countstatus) 
from 
(select post_id,count(*) as countstatus 
  from table_name
   where status='useful' group by post_id)as t1 
 group by t1.id ;

试试这个我假设大众投票意味着状态='有用'

于 2012-10-23T04:42:21.787 回答