从两个表中删除加入?

时间:2011-02-08 14:44:44

标签: mysql

我有两张表如下

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

其中tbl1.id - > tbl2.article_id

如何从两个表中删除旧的< 2008-02-10 00:00:00?

3 个答案:

答案 0 :(得分:10)

请参阅我对类似问题的回答here

总结一下,它看起来像是

 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');

但更好的解决方案是使用on delete cascade的外键约束,如果这是一个选项,那么只需使用适当的where子句从tbl1删除。

答案 1 :(得分:0)

您可以使用triggers

答案 2 :(得分:0)

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

相关问题