这是一个名为posts_votes
的表id|discussion_id|post_id|user_id|vote_sign|
__________________________________________
1 | 1 | 1 | 1 | 1 |
2 | 1 | 1 | 2 | -1 |
3 | 1 | 2 | 3 | 1 |
4 | 1 | 2 | 4 | 1 |
5 | 2 | 3 | 1 | -1 |
6 | 2 | 4 | 2 | 1 |
我想用这些结果创建一个视图:
discussion_id|post_id|score
1 | 2 | 2
2 | 4 | 1
使用:
我正在用群体折磨我的心灵并且有最大但我没有办法做到这一点.. =(
如果有人有想法......
谢谢;)
答案 0 :(得分:2)
使用子查询首先计算得分并为每个discussion_id选择最高得分。然后join
结果集以获得每个discussion_id的最高分数。
select t1.*
from (select discussion_id,post_id,sum(vote_sign) as score
from posts_votes
group by discussion_id,post_id) t1
join (select discussion_id,max(score) as maxscore
from (select discussion_id,post_id,sum(vote_sign) as score
from posts_votes
group by discussion_id,post_id) t
group by discussion_id) t2
on t1.discussion_id = t2.discussion_id and t1.score = t2.maxscore
答案 1 :(得分:1)
select SUBSTRING_INDEX(GROUP_CONCAT(post_id ORDER BY sm DESC), ',', 1) AS top_post, discussion_id, max(score) as score
from (
select discussion_id, post_id, sum(vote_sign) as score
from posts_votes
group by post_id, discussion_id
) c
group by discussion_id
答案 2 :(得分:0)
var b = new B();
b.virtualFunction();
答案 3 :(得分:0)
此处查询对应于您的问题
select
discussion_id,
max(post_id) as max_post_id,
sum(vote_sign) as score
from
posts_votes
group by
discussion_id;