我有两张牌桌和号码分配。 allocation将主列号作为外键。我有一个视图,它给出了分配表中存在的所有数字。然而,我正在寻找反向,寻找UN分配/不在分配表中的数字。我创建了一个NOT EXISTS但结果是空白的。结构和视图如下;
number (table)
NID primary
number
allocation (table)
AID primary
NID Foreign Key to number table
我的观点结构:
CREATE VIEW vNoAllocationNumber AS
SELECT NID
FROM number
WHERE NOT EXISTS (SELECT NID
FROM number, allocation
WHERE allocation.NID = number.NID)
答案 0 :(得分:1)
试试这个:
CREATE VIEW vNoAllocationNumber AS
SELECT NID
FROM number n
WHERE NOT EXISTS (
SELECT 1
FROM allocation a
WHERE a.NID = n.NID
)
;
你也可以利用左连接(除非allocation.NID构成一个AK, 此技术仅适用于联合国指定的NID):
CREATE VIEW vNoAllocationNumber AS
SELECT NID
FROM number n
LEFT JOIN allocation a ON ( a.NID = n.NID )
WHERE a.NID IS NULL
;