Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get join on mysql delete query to work

I know there is more than one question out there that matches this, but I am relatively new to mysql, and I can't seem to make this work using sub quests or the USING key word, plus I find the mysql on line docs a complete mystery.

I started trying to build my DELETE query using a SELECT query as my base and was able to get all the rows that I wanted to delete:

select *
from writings_tags_link
join writing_tags on writing_tags.id = writings_tags_link.tag_id
where writing_tags.tag = 'tag one'

and then just replaced select all with DELETE so:

delete
from writings_tags_link
join writing_tags on writing_tags.id = writings_tags_link.tag_id
where writing_tags.tag = 'tag one'

I gather from both the error message and from other similar posts that you can't use 'ON' to join tables in a delete query, you have to use USING or a sub query. The query I built with USING returns a really strange error, first the query:

DELETE
FROM writings_tags_link
USING writing_tags_link INNER JOIN writing_tags  
WHERE writing_tags.id = writings_tags_link.tag_id 
AND writing_tags.tag ='tag one'

error:

#1109 - Unknown table 'writings_tags_link' in MULTI DELETE

This table does exist, obviously, my original select query returned the desired results. Any help / explanation would be so very appreciated!

Please keep in mind, I'm only trying to delete the data in the linking table.

like image 620
JoeM05 Avatar asked Jan 20 '11 05:01

JoeM05


People also ask

Can we use JOIN IN delete query?

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.

Why full JOIN is not working in MySQL?

MySQL does not support FULL JOIN, so you have to combine JOIN, UNION and LEFT JOIN to get an equivalent. It gives the results of A union B. It returns all records from both tables. Those columns which exist in only one table will contain NULL in the opposite table.

Why delete is not working in MySQL?

Solution. By default, MySQL workbench is started in safe mode, and can't update or delete, without a “WHERE” condition, see the error message. To fix it, in menu, selects “Edit” -> “Preferences” -> “SQL Queries”, uncheck the “Safe Updates” checkbox. Done, try reconnect and issue the delete command again.


1 Answers

Your information is incorrect about requiring the use of the USING keyword in DELETE syntax when using JOINs - the documentation provides examples in the multi-delete section:

DELETE wtl
  FROM WRITINGS_TAGS_LINK wtl
  JOIN WRITING_TAGS wt ON wt.id = wtl.tag_id
 WHERE wt.tag = 'tag one'
like image 194
OMG Ponies Avatar answered Sep 23 '22 15:09

OMG Ponies