-1

我有一个带有客户端和来宾表的数据库。两个表都有 client_icNum 和 guest_icNum。这些客户和客人是同一个人。现在我想知道我应该怎么做才能使client_icNum被识别为与guest_icNum相同?

4

1 回答 1

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
于 2013-10-08T10:11:44.580 回答