MySQL: 为什么我用EXCEPT从原来的选择中排除一些行时出现错误?
MySQL: Why there has an error when I use EXCEPT to exclude some rows from the original selection?
来源:Leetcode 1264
Table:友谊
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user1_id | int |
| user2_id | int |
+---------------+---------+
(user1_id, user2_id) is the primary key for this table.
Each row of this table indicates that there is a friendship relation between user1_id and user2_id.
Table:赞数
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| user_id | int |
| page_id | int |
+-------------+---------+
(user_id, page_id) is the primary key for this table.
Each row of this table indicates that user_id likes page_id.
编写一个 SQL 查询,使用您朋友喜欢的页面向 user_id = 1 的用户推荐页面。它不应该推荐您已经喜欢的页面。
Return 结果 table 任意顺序,无重复。
查询结果格式如下例:
Friendship table:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 3 |
| 2 | 4 |
| 2 | 5 |
| 6 | 1 |
+----------+----------+
Likes table:
+---------+---------+
| user_id | page_id |
+---------+---------+
| 1 | 88 |
| 2 | 23 |
| 3 | 24 |
| 4 | 56 |
| 5 | 11 |
| 6 | 33 |
| 2 | 77 |
| 3 | 77 |
| 6 | 88 |
+---------+---------+
Result table:
+------------------+
| recommended_page |
+------------------+
| 23 |
| 24 |
| 56 |
| 33 |
| 77 |
+------------------+
User one is friend with users 2, 3, 4 and 6.
Suggested pages are 23 from user 2, 24 from user 3, 56 from user 3 and 33 from user 6.
Page 77 is suggested from both user 2 and user 3.
Page 88 is not suggested because user 1 already likes it.
我的代码:
select distinct(l1.page_id) as recommended_page
from Likes l1
left join Friendship f
on l.user_id = f.user2_id
where user1_id = 1
UNION
select distinct(l2.page_id) as recommended_page
from Likes l2
left join Friendship f
on l.user_id = f.user1_id
where user2_id = 1
EXCEPT
select page_id as recommended_page
from Likes
where user_id = 1
虽然except附近有错误。有人可以帮我弄清楚为什么吗?非常感谢!
Leetcode 使用 MySQL 不支持 EXCEPT 关键字。
试试这个 SQL:
select recommended_page from
(
select distinct(l1.page_id) as recommended_page
from Likes l1
left join Friendship f
on l.user_id = f.user2_id
where user1_id = 1
UNION
select distinct(l2.page_id) as recommended_page
from Likes l2
left join Friendship f
on l.user_id = f.user1_id
where user2_id = 1
) x
WHERE recommended_page NOT IN
(
select page_id as recommended_page
from Likes
where user_id = 1
)
来源:Leetcode 1264
Table:友谊
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user1_id | int |
| user2_id | int |
+---------------+---------+
(user1_id, user2_id) is the primary key for this table.
Each row of this table indicates that there is a friendship relation between user1_id and user2_id.
Table:赞数
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| user_id | int |
| page_id | int |
+-------------+---------+
(user_id, page_id) is the primary key for this table.
Each row of this table indicates that user_id likes page_id.
编写一个 SQL 查询,使用您朋友喜欢的页面向 user_id = 1 的用户推荐页面。它不应该推荐您已经喜欢的页面。
Return 结果 table 任意顺序,无重复。
查询结果格式如下例:
Friendship table:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 3 |
| 2 | 4 |
| 2 | 5 |
| 6 | 1 |
+----------+----------+
Likes table:
+---------+---------+
| user_id | page_id |
+---------+---------+
| 1 | 88 |
| 2 | 23 |
| 3 | 24 |
| 4 | 56 |
| 5 | 11 |
| 6 | 33 |
| 2 | 77 |
| 3 | 77 |
| 6 | 88 |
+---------+---------+
Result table:
+------------------+
| recommended_page |
+------------------+
| 23 |
| 24 |
| 56 |
| 33 |
| 77 |
+------------------+
User one is friend with users 2, 3, 4 and 6.
Suggested pages are 23 from user 2, 24 from user 3, 56 from user 3 and 33 from user 6.
Page 77 is suggested from both user 2 and user 3.
Page 88 is not suggested because user 1 already likes it.
我的代码:
select distinct(l1.page_id) as recommended_page
from Likes l1
left join Friendship f
on l.user_id = f.user2_id
where user1_id = 1
UNION
select distinct(l2.page_id) as recommended_page
from Likes l2
left join Friendship f
on l.user_id = f.user1_id
where user2_id = 1
EXCEPT
select page_id as recommended_page
from Likes
where user_id = 1
虽然except附近有错误。有人可以帮我弄清楚为什么吗?非常感谢!
Leetcode 使用 MySQL 不支持 EXCEPT 关键字。
试试这个 SQL:
select recommended_page from
(
select distinct(l1.page_id) as recommended_page
from Likes l1
left join Friendship f
on l.user_id = f.user2_id
where user1_id = 1
UNION
select distinct(l2.page_id) as recommended_page
from Likes l2
left join Friendship f
on l.user_id = f.user1_id
where user2_id = 1
) x
WHERE recommended_page NOT IN
(
select page_id as recommended_page
from Likes
where user_id = 1
)