Delete record comparing values between two tables

0

I have two tables:

produtos
- id_produto

valores
- id_produto

id_produto would be a column, same for both tables.

I need to delete records from the values table, but only those that are NOT also in the products table.

Ex:

I have two products registered, and in the Values table I have the ID of these two products. For some reason I deleted a product from the products table, but it was kept in the values table.

I want to turn off this registry.

How can I do this?

    
asked by anonymous 05.04.2018 / 19:16

1 answer

3

Make a DELETE with a SUB-SELECT :

DELETE FROM valores WHERE id_produto NOT IN (SELECT id_produto FROM produtos);
    
05.04.2018 / 19:20