O
O
only_works2017-08-13 07:18:43
MySQL
only_works, 2017-08-13 07:18:43

Removal of record from the second table on selection from the first table?

Hello.
Suppose there are two tables in the database: t1 and t2. In t1, each entry has a unique id. There are several records in t2 containing a unique id from t1. I need to remove from t2 all records that contain a unique id from t1.
Roughly speaking, I need to save the result of the selection from t1 to some variable, and then delete all records from t2 by the value of this variable. All this needs to be done somehow only in SQL.
Thanks everyone for the replies, I solved this issue using the ANY operator.

DELETE FROM t2 WHERE id = ANY(SELECT id FROM t1 WHERE id = 1 )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2017-08-13
@yarosroman

delete from t2 where id in (select id from t1 where .......)

L
longclaps, 2017-08-13
@longclaps

DELETE t2.* FROM t1
  JOIN t2 ON t1.id = t2.`уникальный id из t1`
WHERE <условие на t1>;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question