如何检查sql中两个表之间的区别?

时间:2017-06-19 09:20:39

标签: mysql sql

我有一个表相关性,其中有2个列。 1. User_id(它有两个用户,一个和两个) 2.评级

*现在我想知道两个用户的评分之间的差异(1,2)。评级差异应为两个。 *

表: “+--------------------------+ | Tables_in_ndcg_reporting | +--------------------------+ | averages | | last_visited_queries | | products | | queries | | query_types | | ratings | | ratings_news | | relevancies | | schema_migrations | | sites | | users | +--------------------------+ 内部相关性:

`+-----+--------+------------+----------+---------+---------------------+---------------------+

| id |评级| product_id | query_id | user_id | created_at | updated_at

| 726 | 5 | 1 | 1 | 2 | 2016-11-24 06:06:12 | 2016-11-24 12:43:07 |

| 727 | 5 | 2 | 1 | 2 | 2016-11-24 06:06:12 | 2016-11-24 12:43:21 |

| 728 | 5 | 3 | 1 | 2 | 2016-11-24 06:06:12 | 2016-11-24 12:43:31 |

| 729 | 5 | 4 | 1 | 2 | 2016-11-24 06:06:12 | 2016-11-24 12:43:32 |

| 730 | 4 | 5 | 1 | 2 | 2016-11-24 06:06:12 | 2016-11-24 12:43:53 |

| 731 | 5 | 6 | 1 | 2 | 2016-11-24 06:06:12 | 2016-11-24 12:43:55 |

| 732 | 4 | 7 | 1 | 2 | 2016-11-24 06:06:12 | 2016-12-28 10:25:52 |

| 733 | 4 | 8 | 1 | 2 | 2016-11-24 06:06:12 | 2016-12-27 12:44:24 |

| 734 | 5 | 9 | 1 | 2 | 2016-11-24 06:06:12 | 2016-11-24 12:44:01 |

| 735 | 4 | 10 | 1 | 2 | 2016-11-24 06:06:12 | 2016-12-28 10:25:53 | `

1 个答案:

答案 0 :(得分:0)

您可能正在寻找自我加入,如下所示:

select a.product_id
from relevencies a inner join relevencies b
on a.product_id = b.product_id and a.user_id <> b.user_id and a.rating >= (b.rating + 2);

如果用户只能对产品进行一次审核,则可以删除a.user_id <> b.user_id条件。