2012-10-06 79 views
0

如何刪除以下查詢返回的記錄?刪除返回的SQL結果

Select HL.User from "Hobby List" HL 
INNER JOIN (Select SUBSTRING(Code,1,10) Code,Count(SUBSTRING(CODE,1,10)) 
AS Occurances from Patient where length(Patient.Code) > 10 
Group By SUBSTRING(Code,1,10) 
Having COUNT(SUBSTRING(Code,1,10)) > 1) P 
on SUBSTRING(HL.User,1,10) = P.Code 
Group By HL.User 

我試圖通過使用「刪除是否存在」的做法,要麼是錯還是我沒做正確,因爲它刪除表中的所有返回的記錄,而不是隻是那些刪除的記錄我的查詢。

感謝

回答

2

試試這個:

DELETE FROM 'Hobby List' 
WHERE User IN 
(

Select HL.User from "Hobby List" HL 
INNER JOIN (Select SUBSTRING(Code,1,10) Code,Count(SUBSTRING(CODE,1,10)) 
AS Occurances from Patient where length(Patient.Code) > 10 
Group By SUBSTRING(Code,1,10) 
Having COUNT(SUBSTRING(Code,1,10)) > 1) P 
on SUBSTRING(HL.User,1,10) = P.Code 
Group By HL.User 

) 
3
DELETE FROM "Hobby List" HL WHERE HL.User IN (your query here) 

這應該這樣做的相當不錯,我這個在其他場景中使用了好幾次。

http://www.w3schools.com/sql/sql_in.asp

+0

感謝那些偉大的工作。我只是不需要在刪除子句中爲業餘愛好列表分配一個名稱HL,它給了我一個錯誤,因爲我在查詢中將它命名了兩次。但很容易理解。 – Trevor