Z
Z
zlogr2016-06-14 16:07:13
PostgreSQL
zlogr, 2016-06-14 16:07:13

How to delete a record in a table, provided that the record belongs to two ids, and you need to delete the record with only one id?

Hello.
I had a "blunt" with such a seemingly simple question.
Please help.
There is a products_taxons table:

product_id | taxon_id
           |
  5	   |  693
  5	   |  694
  5	   |  7
  5	   |  200
  5	   |  305
...

I need, for all products with a taxon with id 694 , to remove the taxon with id 693 .
With such a request, both 693 and 694 will be deleted.
DELETE FROM products_taxons WHERE taxon_id = 693 AND taxon_id = 694 ;

Please tell me how is it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2016-06-14
@zlogr

DELETE FROM products_taxons WHERE taxon_id = 693 AND product_id IN (SELECT DISTINCT(product_id) FROM products_taxons taxon_id = 694)

A
Andrey, 2016-06-14
@VladimirAndreev

SELECT
a.*
FROM products_taxons a
JOIN products_taxons b ON a.product_id = b.product_id
where
b.taxon_id = 694

if this query selects the correct rows - then use DELETE instead of SELECT

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question