sql >> Base de Datos >  >> RDS >> Mysql

MySQL:¿cómo eliminar de la tabla cuando la selección anidada usa esa tabla?

Probablemente esté recibiendo este error:

ERROR 1093 (HY000): You can't specify target table 'table_1' for update in FROM clause.

Debería poder hacer esto usando el DELETE sintaxis en su lugar:

DELETE table_1.*
FROM   table_1,
       table_2,
       table_3
WHERE  table_1.id = table_2.table_1_id
AND    table_2.id = table_3.table_2_id
AND    table_3.id = 5

La consulta anterior debería funcionar, pero como regla general recomendaría usar la sintaxis ANSI JOIN en su lugar:

DELETE table_1.*
FROM   table_1
INNER JOIN table_2 on table_2.table_1_id = table_1.id
INNER JOIN table_3 on table_3.table_2_id = table_2.id
WHERE table_3.id = 5