Sql server 在SQL Server中不存在使用
我有两张桌子Sql server 在SQL Server中不存在使用,sql-server,not-exists,Sql Server,Not Exists,我有两张桌子 Doctype ----------- doctypeid doctype DocumentList ----------------- doclistid (pk), projectid, doctypeid, docfile(binary) 我想从doctypeid表中获取所有projectid和doctypeid,其中doctypeid列表(来自Doctype表)不存在于documentlist表中。如果我理解你的问题,你可能会想使用这样的东西: SELECT
Doctype
-----------
doctypeid
doctype
DocumentList
-----------------
doclistid (pk),
projectid,
doctypeid,
docfile(binary)
我想从
doctypeid
表中获取所有projectid
和doctypeid
,其中doctypeid
列表(来自Doctype
表)不存在于documentlist
表中。如果我理解你的问题,你可能会想使用这样的东西:
SELECT projectid, doctypeid
FROM DocumentList
WHERE doctypeid NOT in(1,2,3,4...n)
如果希望
DocumentList
中的projectid
和doctypeid
列表显示其doctypeid在Doctype
中不存在,请使用以下方法:
SELECT
projectid,
doctypeid
FROM
DocumentList
WHERE
NOT EXISTS(SELECT 1
FROM Doctype
WHERE Doctype.doctypeid = DocumentList.doctypeid)
您是否声明希望所有doctypeid都来自一个doctypeid不在表中的表?如果您先搜索,有很多示例可以说明如何执行此操作:doctypeid是主列表。DocumentList表包含多个doctypeid为的文档。我想获取文档列表表中不存在的所有doctypeid。因此,我可以提醒用户,这些文档仍然丢失。表名已更正。
SELECT
projectid,
doctypeid
FROM
DocumentList
WHERE
NOT EXISTS(SELECT 1
FROM Doctype
WHERE Doctype.doctypeid = DocumentList.doctypeid)