Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem deleting rows using left outer join in mysql

Tags:

mysql

Cant seem to workout what im doing wrong here

SELECT * FROM tbl_imagelabel LEFT OUTER JOIN tbl_image ON tbl_imagelabel.label_id = tbl_image.label_id WHERE tbl_image.label_id is NULL

shows the exact rows I want to delete. but if i change SELECT * FROM to DELETE FROM it doesnt work

DELETE FROM tbl_imagelabel LEFT OUTER JOIN tbl_image ON tbl_imagelabel.label_id = tbl_image.label_id WHERE tbl_image.label_id is NULL

like image 507
James Avatar asked Dec 08 '09 17:12

James


People also ask

How do you delete using LEFT join?

Delete left join table is used to delete rows from the left table that do not have matching records in the right table. Below is the syntax to of deleting rows with a left join that does not have matching rows in another table: Delete table1 from table1 LEFT JOIN table2 ON table1. col_name=table2.

Can we use LEFT join IN delete query?

We can also use the LEFT JOIN clause in the DELETE statement to delete rows in a table (left table) that does not have matching rows in another table (right table). Note that we only put T1 table after the DELETE keyword, not both T1 and T2 tables like we did with the INNER JOIN clause.

Can we use delete with join?

A DELETE statement can include JOIN operations. It can contain zero, one, or multiple JOIN operations. The DELETE removes records that satisfy the JOIN conditions.


1 Answers

For futher reference, on MySQL 5+:

DELETE tbl1.* FROM tbl1 LEFT JOIN tbl2 USING(fld) WHERE tbl2.fld IS NULL;

It'll delete any orphans on TBL1 using fld as join.

GL Paulo Bueno

like image 66
Paulo Bueno Avatar answered Sep 25 '22 01:09

Paulo Bueno