SQL Server 如何选择值不存在于同一数据库的另一列中?

w1jd8yoj  于 2022-11-21  发布在  其他
关注(0)|答案(3)|浏览(144)

我要选择具有GUID但不存在于parentGUID中记录
enter image description here
我试过这个代码

select code, GUID, ParentGUID
from ac00
where NOT EXISTS (select ParentGUID from ac00 where GUID <> ParentGUID)
order by Code asc
klsxnrf1

klsxnrf11#

你们很接近,但没有化名.

select code,GUID,ParentGUID 
from ac00 t1 
where NOT EXISTS (select * from ac00 t2 
   where t2.GUID = t1.ParentGUID) 
order by Code asc;
dgiusagp

dgiusagp2#

1-类型字段GUID,ParentGUID转换为int,int 2-GUID上得UNIQUE CLUSTERED INDEX,ParentGUID上得NONCLUSTERED INDEX
在答案中写入SQL代码

8hhllhi2

8hhllhi23#

您可以用更快的方式来完成它。连接通常比内部查询快得多

select ac1.Code,ac1.GUID,ac1.ParentGUID 
from ac00 ac1 LEFT OUTER JOIN dbo.ac00 ac2
ON ac2.Guid = ac1.ParentGuid
WHERE ac2.Code IS NULL

相关问题