2011-07-19 114 views
1

我想刪除由該選擇查詢返回的任何東西:從sql server 2000中選擇刪除?

SELECT u.* 
FROM (
     SELECT userName, groupId, MAX(userId) AS maxId 
     FROM userTable 
    where userName <> '' and userName is not null 
     GROUP BY 
       userName, groupId 
     HAVING COUNT(*) > 1 
     ) q 
JOIN userTable u 
ON  u.userName = q.userName 
     AND u.groupId= q.groupId 
     AND u.userId <> q.maxId) 

我怎樣才能做到這一點?

回答

5

只是更換SELECT u.*DELETE U

+0

+ 1這是一種更優雅的方式 –

10

結構您刪除這樣的:

DELETE U 
FROM U ... 
JOIN Q ... 
WHERE Foo ... 

或者,如果你已經在手漂亮的查詢,你可以這樣做:

DELETE Usertable WHERE userId IN (
    SELECT UserID FROM ... /* big complex query here */ 
)