2017-06-19 90 views
-2

我有一个表相关性,其中有2个colums。 1. USER_ID(它有两个用户,一个和两个) 2.等级如何检查sql中两个表之间的区别?

*现在我想知道两个用户(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

这是这里的2个表格(在问题标题中提到)?你能否提供这两个表格的一些样本数据,以及结果应该是什么样子的样本? –

+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条件。

+0

嘿,我添加了数据。 –