表:
users: id INT
items: id INT
setid INT
sets: id INT
relationships: userid INT
itemid INT
relationship ENUM('owner', 'participant')
给定 auserid
和 a setid
,我们需要生成集合中所有项目的列表,以及用户与每个项目的关系(如果存在关系)。即结果将
setid itemid relationship
---------------------------------
1 1 NULL
1 2 owner
....
以下不起作用,因为第二个where
子句消除了relationship
为空的行:
select
sets.id as setid,
items.id as itemid,
relatonships.relationship as relationship
from sets
inner join items on sets.id = items.setid
left join relationships on relationships.itemid = items.id
where
sets.id = 5
and relationships.userid = 27
但是,第二个where
子句会删除该项目与给定用户之间不存在关系的行。如何通过单个查询来完成?