MySQL : delete from table that is used in the where clause
Posted
by Eric
on Stack Overflow
See other posts from Stack Overflow
or by Eric
Published on 2010-05-10T20:05:01Z
Indexed on
2010/05/10
20:14 UTC
Read the original article
Hit count: 205
I am writing a small script to synchronize 2 MySQL tables ( t1 to be 'mirrored' to t2 )
In a step I would like to delete rows inside t2 that has been delete in t1 with the same id.
I tried this query :
delete from t2 where t2.id in
( select t2.id left join t1 on (t1.id=t2.id) where t1.id is null )
But Mysql forbid me to use t2 in the same time in the delete and in the select (sound logical by the way)
Of course, I can split the query into 2 queries : first select IDs, then delete rows with these IDs.
My question : do you have a cleaner way to delete row from t2 that does not exist anymore in t1 ? with one query only ?
© Stack Overflow or respective owner