我有2个表:edges
和users
edges
(约束:id1< id2):
id1
name1
id2
name2
users
:
id
name
我想获得只有edge(id1,name1,id2,name2)的结果集,以便BOTH id1和id2在users表中。这看起来很简单,但是我遇到了麻烦。我的尝试:
SELECT
e.id1 AS id1,
e.name1 AS name1,
e.id2 AS id2,
e.name2 AS name2
FROM
edges AS e,
users AS u
WHERE u.id = e.id1
UNION
SELECT
e.id1 AS id1,
e.name1 AS name1,
e.id2 AS id2,
e.name2 AS name2
FROM
edges AS e,
users AS u
WHERE u.id = e.id2
任何指针?
答案 0 :(得分:1)
尝试使用两个INNER JOINS:
SELECT * FROM edges e
JOIN users u1 ON e.name1 = u1.name
JOIN users u2 ON e.name2 = u2.name
答案 1 :(得分:1)
尝试一下:
select e.id1, e.name1, e.id2, e.name2 from edges e
join users u1 on e.id1 = u1.id
join users u2 on e.id2 = u2.id
答案 2 :(得分:0)
也许是这样的:
SELECT
e.id1 AS id1,
e.name1 AS name1,
e.id2 AS id2,
e.name2 AS name2
FROM
edges AS e
WHERE EXISTS
(
SELECT
NULL
FROM
users AS u
WHERE
u.id = e.id1
AND u.id = e.id2
)