Question

I have to delete rows from slaveTable that have no more relation with masterTable table (dead relations, removed from masterTable).

I came up with the statement:

DELETE
FROM   slaveTable AS s
LEFT JOIN 
       masterTable AS m 
ON   ( s.key1 = m.key1
AND    s.key2 = m.key2 )
WHERE  m.key1 IS NULL

SQL DELETE with JOIN another table for WHERE condition shows this way as a solution... but won't work for me. Because of my lack of reputation I can't answer there.

Was it helpful?

Solution

specify the table where you want to delete the rows.

DELETE s                    -- << HERE: s (alias of slaveTable )
FROM   slaveTable AS s
LEFT JOIN 
       masterTable AS m 
ON   ( s.key1 = m.key1
AND    s.key2 = m.key2 )
WHERE  m.key1 IS NULL
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top