简体   繁体   中英

Delete from two tables with join?

I have two tables as follows

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 older is < 2008-02-10 00:00:00 ?

See my answer to a similar question here .

To summarize, it would 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 better solution would be a foreign key constraint with an on delete cascade, if that is an option, then just delete from tbl1 with the appropriate where clause.

你可以使用触发器

最简单的方法:你应该使用FOREIGN KEYS和ON DELETE CASCADE。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM