Delete from two tables in one query
P粉401901266
2023-08-27 13:45:48
<p>I have two tables in MySQL</p>
<pre class="brush:php;toolbar:false;">#messages table :
messageid
messagetitle
.
.
#usersmessages table
usersmessageid
messageid
userid
.
.</pre>
<p>Now if I want to delete from the message table I can. But when I delete the message by messageid, the record still exists on usersmessage and I have to delete from both tables immediately. </p>
<p>I used the following query: </p>
<pre class="brush:php;toolbar:false;">DELETE FROM messages LEFT JOIN usersmessages USING(messageid) WHERE messageid='1' ;</pre>
<p>Then I test</p>
<pre class="brush:php;toolbar:false;">DELETE FROM messages , usersmessages
WHERE messages.messageid = usersmessages.messageid
and messageid='1' ;</pre>
<p>But these two queries did not complete the task. </p>
Translation: Delete from table messages, where messageid = 1, if table uersmessages has messageid = messageid of table messages, then delete the rowUser message table.
Can't they be separated by semicolons?
or
Just use
INNER JOIN
as shown below