2017-01-30 58 views
2

我正在使用MS Access 我需要刪除作爲此查詢結果的所有Table1.ids。我想選擇Table1.id但我得到了一個錯誤說:你試圖執行一個查詢,不包括指定表達式「ID」作爲聚集函數的一部分」用Group BY選擇後刪除ID

SELECT 
Table1.code1,Table1.code2 ,COUNT(*) AS count 
FROM 
Table1 LEFT JOIN Table2 ON Table1.id=Table2.id 
Where Table2.id IS NULL 
GROUP BY 
Table1.code1, 
Table1.code2 
Having 
COUNT(*) > 1 

任何幫助表示讚賞

回答

1

呦需要使用in運營商在查詢如果您需要刪除所有符合條件的行,那麼你可以使用你的table1code1列如下:

Delete from Table1 where code1 in 
(
    SELECT 
    Table1.code1 
    FROM 
    Table1 LEFT JOIN Table2 ON Table1.id=Table2.id 
    Where Table2.id IS NULL 
    GROUP BY 
    Table1.code1, 
    Table1.code2 
    Having 
    COUNT(*) > 1 
)