Delete record by comparing values between two tables

Asked

Viewed 1,443 times

0

I have two tables:

produtos
- id_produto

valores
- id_produto

id_produto would be a column, equal for the two tables.

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

Ex:

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

I want to erase that record there.

How can I do that?

1 answer

4


Make a DELETE with a SUB-SELECT:

DELETE FROM valores WHERE id_produto NOT IN (SELECT id_produto FROM produtos);
  • No need for Inner Join there?

  • Hi @Wendler no need no

  • @Wendler is not possible to do INNER JOIN in the DELETE.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.