Query MySql delete from

4

In the database I have.

Coches(matricula,año,modelo)
Es_Dueno(id_conductor,matricula)
Persona(id_conductor, nombre, direccion)

and I want to delete the model car Mazda of the client Santos

I made the following query:

DELETE FROM coches
    WHERE coches.matricula = (
        SELECT coches.matricula 
        FROM persona as A
        INNER JOIN es_dueno as B ON  A.id_conductor = B.id_conductor
        INNER JOIN coches ON B.matricula = coches.matricula 
        WHERE A.nombre ='Santos' AND coches.modelo = 'Mazda')

And I get the following error:

  

1093 - You can not specify target table 'Ej1_cars' for update in FROM clause

    
asked by Tin Rosso 24.11.2016 в 16:40
source

2 answers

5

Based on the response of @Pablo Claus, what you can do is to get the condition coches.modelo = 'Mazda' out of the subquery:

DELETE FROM coches
    WHERE coches.matricula IN (
        SELECT B.matricula 
        FROM persona as A
        INNER JOIN es_dueno as B ON  A.id_conductor = B.id_conductor
        WHERE A.nombre ='Santos')
    AND coches.modelo = 'Mazda'
    
answered by 24.11.2016 / 16:55
source
4

The MySQL guide says that

  

You can not delete from a table and select from the same table in a subquery.

You can not delete from a table and use the same table within the subquery.

link

So, one option would be to obtain the registration first and then delete it in another query.

    
answered by 24.11.2016 в 16:46