0
請考慮這個查詢:存在問題的where子句中返回不正確的結果
SELECT tesd.State_Code,
tesd.City_Code,
tesd.Row_ID,
tesd.Qsno,
tesd.Total_Period,
tesd.Current_Period,
tesd.Week,
tesd.Block_No,
tesd.Family_ID,
tesd.Line_ID,
tesd.Page_ID
INTO #tmp
FROM Specification_Master tesm
INNER JOIN Specification_Details tesd
ON tesd.Master_Id = tesm.Id
WHERE tesm.[Year] = 2000
AND tesm.[Month] = 10
AND tesd.City_Code IN ('001')
我從2個表查詢一些數據和#tmp
插入他們。後來我想從其他2個表中選擇數據,並檢查中的一個表在#tmp
tbale值:
SELECT *
FROM tbl_Details D
INNER JOIN tbl_Master tem
ON D.ID_Master = tem.Id
WHERE D.Period <= 5
AND EXISTS (
SELECT Row_ID
FROM #tmp tm
WHERE tm.Current_Period > 1
AND tm.State_Code = tem.State_Code
AND tm.City_Code = tem.City_Code
AND tm.Qsno = tem.Qsno
)
AND D.[Status] > 2
當我運行此查詢我只有一行,但是當我改變EXISTS
到NOT EXISTS
我得到了更多的rows.I seperatly運行此查詢:
SELECT Row_ID
FROM #tmp tm,tbl_Master tem
WHERE tm.Current_Period > 1
AND tm.Ostan_Code = tem.State_Code
AND tm.City_Code = tem.City_Code
AND tm.Porseshname_ID = tem.Qsno
並返回30行。爲什麼Exists
有這樣的行爲?
你如何分別運行第三個?在這裏沒有定義別名。 – onof 2012-02-03 21:36:39
謝謝我糾正你的第二個查詢中的 – Arian 2012-02-03 21:38:17
我看到聯合tbl_details。只是想知道有多少行是查詢返回out exists塊。 – AJP 2012-02-03 21:41:51