Reputation: 22393
I want to delete anything that is returned by this select query:
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)
How can I do this?
Upvotes: 1
Views: 3359
Reputation: 108356
Structure your delete like this:
DELETE U
FROM U ...
JOIN Q ...
WHERE Foo ...
Or, if you already have a spiffy query in hand, you can do this:
DELETE Usertable WHERE userId IN (
SELECT UserID FROM ... /* big complex query here */
)
Upvotes: 10