我有一个包含客户端和来宾表的数据库。两个表都有client_icNum和guest_icNum。这些客户和客人是同一个人。 现在我想知道我应该做什么来使client_icNum被识别为与guest_icNum相同?
答案 0 :(得分:1)
首先查询:
让所有客户都在客人
SELECT c.*
FROM client c
WHERE EXISTS(
SELECT 'X'
FROM guest g
WHERE c.client_idNum = g.guest_icNum
)
可替换地:
SELECT c.*
FROM client c
JOIN guest g
ON c.client_idNum = g.guest_icNum
第二次查询:
让所有客户都不在客人中:
SELECT c.*
FROM client c
WHERE NOT EXISTS(
SELECT 'X'
FROM guest g
WHERE c.client_idNum = g.guest_icNum
)
可替换地:
SELECT c.*
FROM client c
LEFT OUTER JOIN guest g
ON c.client_idNum = g.guest_icNum
WHERE g.guest_icNum IS NULL