就像标题所示,我在排除2个联合查询中的重复值时遇到了问题。
我的查询看起来有点复杂,所以我将尝试通过另一个例子来简化它:
SELECT u.name
FROM user u
LEFT JOIN user_hobby uh on uh.user_id = u.id
JOIN teams t on t.id = u.team
JOIN group g on g.id = uh.group and g.name = 'average'
where u.active = 1 and u.team is not null
UNION
SELECT u.name
FROM user u
LEFT JOIN user_hobby uh on uh.user_id = u.id
JOIN teams t on t.id = u.team
where u.active = 1 and u.team is null
所以基本上,两个查询都给了我正确的答案,但重复值
第一个查询输出: 本,米歇,安娜,罗伯特
第二个查询输出: 让,查尔斯,安娜,罗伯特
如果我可以省略anna和robert,如果它已经出现在第一个查询中,我如何在UNION
中使用SELECT out('my_edge') FROM Person
SELECT expand(in('my_edge')) FROM Person
SELECT max('my_field') FROM Person
SELECT count(*) FROM Person
?
答案 0 :(得分:0)
UNION已删除重复的条目。
(
SELECT u.name
FROM user u
LEFT JOIN user_hobby uh on uh.user_id = u.id
JOIN teams t on t.id = u.team
JOIN group g on g.id = uh.group and g.name = 'average'
where u.active = 1 and u.team is not null
)
UNION
(
SELECT u.name
FROM user u
LEFT JOIN user_hobby uh on uh.user_id = u.id
JOIN teams t on t.id = u.team
where u.active = 1 and u.team is null
)
答案 1 :(得分:0)
您是否尝试在SELECT中使用Distinct?
答案 2 :(得分:0)
尝试 - 从(查询1 UNION查询2)x
中选择不同的x.name