我有一張名爲Documents
的表,其中有以下幾列。查詢找不到匹配的文檔類型ID
DocID,DocTypeID,Invoice,Invoice_Date
我要找的是讓出現在DocTypeID 5bef8666
,但不會出現在DocTypeID 923847f9
所有發票。
例
DocID,DocTypeID,Invoice,Invoice_Date
00001,923847f9, 00001 ,24/01/2016
00002,923847f9, 00002 ,24/01/2016
00003,923847f9, 00003 ,24/01/2016
00004,923847f9, 00004 ,24/01/2016
00005,5bef8666, 00005 ,24/01/2016
00001,5bef8666, 00001 ,24/01/2016
00002,5bef8666, 00002 ,24/01/2016
00003,5bef8666, 00003 ,24/01/2016
00004,5bef8666, 00004 ,24/01/2016
結果
DocID, DocTypeID, Invoice, Invoice_Date
00005, 5bef8666, 00005, 24/01/2016
這是我至今沒有結果嘗試。
SELECT *
FROM Documents d1
WHERE d1.DocTypeID = ' 5bef8666'
AND NOT EXISTS (SELECT 1
FROM Documents d2
WHERE d2.DocTypeID = '923847f9'
and d2.Invoice = d1.Invoice);
在此先感謝您。
請發表你已經嘗試了嘗試,並解釋哪裏出了問題 – Peter
@ user3309798最簡單的where子句'如果DocTypeID ='5bef8666'' – tchelidze
SQL中where子句的用法是什麼? – Anil