Delete rows from table inner join

sioesi

I'm having a problem, I explain:

I have a table called Tipo_Base that contains Id, nombre_tipo_base

I have a table called Tipo_Base_Lista that contains Id, id_tipo_base, and lista_id

I have a table called Modelo_unidad that contains Id, nombre_modelo and id_tipo_base

I have a table called Modelo_Lista that contains Id, id_modelo, id_lista

Each id_lista of the table modelo_lista MUST be present in the table tipo_base_lista, then, when I delete a id_lista from the table tipo_base_lista, it must also be deleted from the table modelo_lista.

Try the following:

DELETE Tbl_modelo_lista
    FROM 
        Tbl_modelo_lista 
        INNER JOIN Tbl_modelo_unidad as MU ON MU.id_modelo = Tbl_modelo_lista.id_modelo
        INNER JOIN Tbl_tipo_base_lista as TBL ON TBL.id_tipo_base = MU.id_tipo_base
    WHERE
        TBL.id_lista <> Tbl_modelo_lista.id_lista
Gordon Linoff

I think the logic you want is more like this:

DELETE ml
    FROM Tbl_modelo_lista ml INNER JOIN
         Tbl_modelo_unidad mu
         ON mu.id_modelo = ml.id_modelo LEFT JOIN
         Tbl_tipo_base_lista tbl
         ON tbl.id_tipo_base = mu.id_tipo_base AND
            tbl.id_lista = ml.id_lista
    WHERE tbl.id_lista IS NULL;

Normally, the way to implement this logic is with a cascading delete constraint. In your case, I'm not sure this would work. Cascading triggers are useful when you need to propagate changes from the reference table outwards. They don't keep track of incoming references and delete a record when there are no references.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

sql Inner join delete

From Dev

Delete data from two tables with INNER JOIN

From Dev

Delete from table on join

From Dev

How to Delete Top(N) rows with an inner join?

From Dev

Delete rows from table

From Dev

Getting count from inner join table

From Dev

How delete table inner join with other table in Sqlite?

From Dev

INNER JOIN results in no rows

From Dev

Codeigniter cannot delete rows from database table using join method

From Dev

Oracle, LEFT OUTER JOIN not returning all rows from left table, instead behaving like INNER JOIN

From Dev

SQlite delete inner join

From Dev

Delete from SQLite table with join to link table

From Dev

Inner join rows not affected

From Dev

Delete from 2 tables using INNER JOIN

From Dev

Delete rows from table using JOIN - SQL Server

From Dev

Delete X rows from table

From Dev

Select distinct rows from a table with an inner join

From Dev

Getting count from inner join table

From Dev

How delete table inner join with other table in Sqlite?

From Dev

INNER JOIN results in no rows

From Dev

How to delete rows from a table based on a join with another table?

From Dev

How to delete rows from two tables using INNER JOIN in mysql?

From Dev

Delete data from a single table from inner join result

From Dev

Enumerating rows in a inner join

From Dev

deleting records from a table where there is an inner join

From Dev

mySQL - Delete rows in table with Inner Join

From Dev

SQL How to Delete Data from Table Using INNER JOIN

From Dev

Delete using Select Inner Join from Same Table

From Dev

MySQL Inner Join with No Rows