P.B*_*key 3 sql t-sql sql-server-2000
我有一个问题
SELECT *
FROM table1
WHERE
documentId in
(
--select items from a second table where a third column happens to be null
select documentId from table2 t2 inner join table1 t1
on t1.documentId = t2.documentId and t1.itemId = t2.ItemId
WHERE t1.someOtherColumn is null
)
and itemId in
(
--similar query as above, just selecting itemId now
select itemId from table2 t2 inner join table1 t1
on t1.documentId = t2.documentId and t1.itemId = t2.ItemId
WHERE t1.someOtherColumn is null
)
order by 1
Run Code Online (Sandbox Code Playgroud)
鉴于table1具有复合PK = documentId + itemId,这是否足以仅从table1中选择唯一的值?我担心在documentId存在且itemsId存在的情况下可能会出现这种情况,但由于它们没有一起查看,因此可以进行不正确的选择.
例如,
我们假设存在一个值
documentId = 1,itemId = 1.
我们假设没有复合键
documentId = 1,itemId = 1.
我不想要复合键
[documentId = 1和itemId = 3]
被包括.
我也不想要复合键
[documentId = 2和itemId = 1]
被包括.
如果稍后添加了复合键(现在不存在)
[documentId = 1和itemId = 1]
然后应该包括在内.
这样做无需使用连接或两个INs
SELECT *
FROM table1
WHERE exists
(
SELECT 1
FROM table2
WHERE someOtherColumn is null
AND table1.documentId = table2.documentId
AND table1.itemId = table2.ItemId
)
ORDER BY 1
Run Code Online (Sandbox Code Playgroud)
你是正确的,假设它可能导致错误的结果.你可以用这个:
SELECT table1.*
FROM table1
WHERE someOtherColumn IS NULL
AND (documentId, itemId ) IN
(
SELECT documentId, itemId
FROM table2 t2 --- no need for table1 again here
)
ORDER BY 1
Run Code Online (Sandbox Code Playgroud)
更新:我认为上述内容并不适用于SQL-Server,仅适用于Postgres(和Oracle?).无论如何,这应该适用于大多数系统:
SELECT table1.*
FROM table1
WHERE someOtherColumn IS NULL
AND EXISTS
(
SELECT documentId, itemId FROM table2 t2
WHERE table2.documentId = table1.documentId
AND table2.itemId = table1.itemId
)
ORDER BY 1
Run Code Online (Sandbox Code Playgroud)