MySQL Delete records from 2 tables

I want to delete information in two different tables in 1 query based on identifier.

I tried several solutions to accomplish this task, but still haven't completed what I'm trying to do.

Table 1 - Contents

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

Table 2 - Voices

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

I want to delete the content line based on its identifier and any or all votes (there may be 0 vote entries). I DO NOT want to use transactions, cascading deletes, or use 2 different queries.

Which is Best - LEFT JOIN? INNER JOIN?

Any help here would be greatly appreciated.

+3
source share
5 answers
DELETE Content, Votes
FROM Content
LEFT JOIN Votes
ON Votes.ContentID = Content.ContentID
WHERE Content.ContentID = ?
+7

, . .

- 2 , .

+2
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;

3 , , .

+1

DELETE WHERE. ORDER BY LIMIT DELETE . table_references , . 12.2.7.1, "JOIN Syntax".

DELETE MySQL 4.0.0. MySQL 4.0.2.

, FROM. , FROM ( USING). , , :

DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id=t2.id AND t2.id=t3.id;

:

DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id=t2.id AND t2.id=t3.id;

, t1 t2.

" http://dev.mysql.com/doc/refman/4.1/en/delete.html"

+1

Without JOINS, the simplest thing I could come up with when I was looking for a solution was DELETE a.*,b.* FROM table1 AS a, table2 AS b WHERE a.id = b.id AND a.field = 1

0
source

Source: https://habr.com/ru/post/1753733/


All Articles