Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete from two tables with join?

Tags:

mysql

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 ?

like image 872
georgevich Avatar asked Feb 08 '11 14:02

georgevich


1 Answers

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.

like image 69
Brandon Horsley Avatar answered Oct 18 '22 14:10

Brandon Horsley