SQL 与 GUID 具有 NOT IN 和 WHERE 关系的查询
SQL query with NOT IN and WHERE relation with GUIDs
有两个tableDocument
和DocumentPos
。在 Document
中有列 GUID
,在 DocumentPos
中有列 DocumentGUID
,它指的是 table Document
.
I want to have every row in DocumentPos
where its DocumentGUID
has now
row in Documents'
GUID
.
我有这个查询 returns 0 行:
select *
FROM Document d,
DocumentPos dp
WHERE d.GUID = dp.DocumentGUID
AND dp.DocumentGUID NOT IN (
SELECT d.GUID
FROM Document
)
但是当我执行 select * from documentpos
时,它 returns 例如一行 DocumentGUID
= B479BCB72334424DAC1B7CC26880DAB8
。而这个DocumentGUID
是NOT IN
tableDocument
作为GUID
。
但是 select * from Document where GUID = 'B479BCB72334424DAC1B7CC26880DAB8'
returns 0 行。
我想像这样构建查询,因为它应该成为 DELETE
语句:
DELETE dp
FROM Document d,
DocumentPos dp
WHERE d.GUID = cp.DocmentGUID
AND dp.DocumentGUID NOT IN (
SELECT d.GUID
FROM Document
)
第二个问题我也想知道:
Why is in the brackets not FROM d
possible and only FROM Document
?
NOT IN
对于 NULL
是棘手的。您可以改用 NOT EXISTS
,这是 null 安全的。另外,我不明白为什么你需要在外部查询中引入 document
table。
我想你想要:
select *
from documentpos dp
where not exists (
select 1 from document d where d.guid = dp.documentguid
)
您可以将其转换为删除语句,如下所示:
delete dp
from documentpos dp
where not exists (
select 1 from document d where d.guid = dp.documentguid
)
我能理解的是您想从 DocumentPos table 中删除 Main table Document 中不存在的数据。您可以使用以下查询:
DELETE dp FROM DocumentPos dp
left join Document d on d.GUID = dp.DocumentGUID
where d.GUID is null
有两个tableDocument
和DocumentPos
。在 Document
中有列 GUID
,在 DocumentPos
中有列 DocumentGUID
,它指的是 table Document
.
I want to have every row in
DocumentPos
where itsDocumentGUID
has now row inDocuments'
GUID
.
我有这个查询 returns 0 行:
select *
FROM Document d,
DocumentPos dp
WHERE d.GUID = dp.DocumentGUID
AND dp.DocumentGUID NOT IN (
SELECT d.GUID
FROM Document
)
但是当我执行 select * from documentpos
时,它 returns 例如一行 DocumentGUID
= B479BCB72334424DAC1B7CC26880DAB8
。而这个DocumentGUID
是NOT IN
tableDocument
作为GUID
。
但是 select * from Document where GUID = 'B479BCB72334424DAC1B7CC26880DAB8'
returns 0 行。
我想像这样构建查询,因为它应该成为 DELETE
语句:
DELETE dp
FROM Document d,
DocumentPos dp
WHERE d.GUID = cp.DocmentGUID
AND dp.DocumentGUID NOT IN (
SELECT d.GUID
FROM Document
)
第二个问题我也想知道:
Why is in the brackets not
FROM d
possible and onlyFROM Document
?
NOT IN
对于 NULL
是棘手的。您可以改用 NOT EXISTS
,这是 null 安全的。另外,我不明白为什么你需要在外部查询中引入 document
table。
我想你想要:
select *
from documentpos dp
where not exists (
select 1 from document d where d.guid = dp.documentguid
)
您可以将其转换为删除语句,如下所示:
delete dp
from documentpos dp
where not exists (
select 1 from document d where d.guid = dp.documentguid
)
我能理解的是您想从 DocumentPos table 中删除 Main table Document 中不存在的数据。您可以使用以下查询:
DELETE dp FROM DocumentPos dp
left join Document d on d.GUID = dp.DocumentGUID
where d.GUID is null