我有一个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个状态投票,所以我可以在帖子桌上找到这篇文章的所有者。谢谢!!
答案 0 :(得分: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
)
基本上,我不建议在这里使用限制,因为某些Post_ID
可能有相同的票数。
答案 1 :(得分: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 ;
试试这个 我假设民众投票意味着状态='有用'