I have a table in mysql like this:
id | userID | idRef | userIDRef |
---|---|---|---|
1 | 30 | 2 | 100 |
2 | 100 | 0 | 0 |
3 | 30 | 6 | 100 |
5 | 30 | 7 | 100 |
7 | 100 | 0 | 0 |
10 | 30 | 8 | 100 |
In php I want to remove all rows for userID = 30 where userIDRef = 100 and idRef not exist in table. In this example I want to remove rows with idRef = 6 and idRef = 8 because not exist inside table
DELETE FROM table
WHERE userID = 30
AND idRef != 0
AND idRef NOT IN (
SELECT id
FROM table
WHERE userID = 100
AND)
2
Answers
Create test data:
Now do the deletion:
… and verify:
See example