2012-04-06 120 views
-3

如何更改此SELECT,因此它是用於刪除的條件?使用GROUP BY進行刪除

SELECT COUNT(empid), empid, status, deptid from tableA 
GROUP BY empid, status, deptid HAVING status is null and deptid = 5 

回答

2

你想刪除什麼?

如果簡單地在DEPTID 5 NULL狀態記錄,使用

WHERE status is NULL and DeptID = 5 

不是很清楚,如果你正在尋找更多的比...

+0

實際上,我認爲就是這樣。謝謝。 – 4thSpace 2012-04-06 16:58:25

1

與GROUP BY DELETE可能無法正常工作,我猜。

您可以嘗試以下操作,並查看它是否有效。

DELETE 
FROM tableA 
WHERE status is null AND deptid=5 
1
DELETE FROM tableA 
WHERE status is null AND deptid = 5 
1
Delete from tableA a, 
(SELECT COUNT(empid) as count, empid, status, deptid from tableA 
    GROUP BY empid, status, deptid HAVING status is null and deptid = 5) as b 
where a.empid = b.empid 
and a.status = b.status 
and a.deptid = b.deptid 
and b.count > 5 

將類似的東西是你在找什麼?它會刪除所有屬於組的一部分的所有記錄,並且計數> 5

否則,您確實沒有刪除組中的值,並且可以使用簡單的刪除;

Delete from tableA 
where status is null 
and deptid = 5