Delete from two input tables?

I have two tables:

tbl1 tbl2 id article_id title, image whole_news tags, author, older (datetime) 

where tbl1.id → tbl2.article_id

How to delete records from both tables, where the older one is <2008-02-10 00:00:00?

+4
source share
3 answers

See my answer to a similar question here .

To summarize, it will look like

  delete s, r from tbl1 s left join tbl2 r on s.id = r.article_id where s.older < str_to_date('2008-02-10 00:00:00', '%Y-%m-%d %H:%i:%S'); 

But the best solution would be to restrict the foreign key with the delete cascade, if this is an option, then simply delete from tbl1 with the corresponding where clause.

+9
source

You can use triggers

0
source

The easiest way: you should use FOREIGN KEYS WITH ON DELETE CASCADE.

0
source

Source: https://habr.com/ru/post/1338870/


All Articles