EXISTS
結合の代わりに2 つのクエリを書き直すと、最初に (ここでは主キーであるDISTINCT
と仮定します) を削除し、次にand (とも呼ばれます) 操作を明確にできます。uid
Users
INTERSECT
EXCEPT
MINUS
クエリ 1:
SELECT U.uid
FROM users U
WHERE U.status = '1'
AND EXISTS
( SELECT *
FROM friends F
WHERE U.uid = F.friend_two
AND F.friend_one = '1'
AND F.ROLE = 'fri'
) ;
クエリ 2:
SELECT U.uid
FROM users U
WHERE U.status = '1'
AND EXISTS
( SELECT *
FROM friends F
WHERE U.uid = F.friend_one
AND F.friend_two = '1'
AND F.ROLE = 'fri'
) ;
クエリ 3:INTERSECT
SELECT U.uid
FROM users U
WHERE U.status = '1'
AND EXISTS
( SELECT *
FROM friends F
WHERE U.uid = F.friend_two
AND F.friend_one = '1'
AND F.ROLE = 'fri'
)
AND EXISTS
( SELECT *
FROM friends F
WHERE U.uid = F.friend_one
AND F.friend_two = '1'
AND F.ROLE = 'fri'
) ;
クエリ 4: EXCEPT
( MINUS
)
SELECT U.uid
FROM users U
WHERE U.status = '1'
AND EXISTS
( SELECT *
FROM friends F
WHERE U.uid = F.friend_two
AND F.friend_one = '1'
AND F.ROLE = 'fri'
)
AND NOT EXISTS -- notice the NOT here
( SELECT *
FROM friends F
WHERE U.uid = F.friend_one
AND F.friend_two = '1'
AND F.ROLE = 'fri'
) ;