给定两个用户,选择两个用户评定的每个帖子,以及每个用户对每个帖子的评级

时间:2012-03-09 04:20:22

标签: mysql sql

说我有下表:

create table ratings (
user_id int unsigned not null,
post_id int unsigned not null,
rating set('like','dislike'),
primary key (user_id, post_id)
);

还有两个用户,其中ID为1和2.我想了解他们在评分中达成了多少,所以我需要的是选择用户评价的所有帖子以及每个用户的评分对每个帖子。类似的东西:

+---------+---------+---------+
| post_id | rating1 | rating2 |
+---------+---------+---------+
|       1 | like    | dislike |
|       2 | like    | like    |
|       5 | like    | dislike |
|       6 | dislike | dislike |
+---------+---------+---------+

感谢任何线索,谢谢!

1 个答案:

答案 0 :(得分:2)

在帖子ID中加入您的评分表,并从每个表格中选择评分,限制用户:

SELECT r1.post_id, r1.rating AS rating1, r2.rating AS rating2
FROM ratings r1
JOIN ratings r2 ON r1.post_id=r2.post_id AND r1.user_id!=r2.user_id
WHERE r1.user_id=1 AND r2.user_id=2