Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Delete Records from 2 Tables

I'm looking to delete information in two different tables in 1 query, based on an ID.

I've tried several solutions on here to accomplish this task but still have not accomplished what I'm trying to do.

Table 1 - Content

---------- ---------
 ContentID | Content
--------------------

Table 2 - Votes

---------------------------
 VoteID | ContentID | Vote 
---------------------------

I want to delete the content row based on its ID and any or all votes (there could be 0 vote records). I do NOT want to use transactions, cascading deletes, or use 2 different queries.

What is best here - a LEFT JOIN? INNER JOIN?

Any help here would be greatly appreciated.

like image 939
barfoon Avatar asked Dec 09 '22 15:12

barfoon


2 Answers

DELETE Content, Votes
FROM Content
LEFT JOIN Votes
ON Votes.ContentID = Content.ContentID
WHERE Content.ContentID = ?
like image 164
Mike Avatar answered Dec 14 '22 23:12

Mike


If you have a relation you can try with this ON DELETE CASCADE option.

Another option is to create a stored procedure and do the delete in 2 steps but with only one server call.

like image 33
munissor Avatar answered Dec 15 '22 00:12

munissor