Delete from two tables in one query
P粉401901266
P粉401901266 2023-08-27 13:45:48
0
2
483
<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>
P粉401901266
P粉401901266

reply all(2)
P粉164942791
DELETE a.*, b.* 
FROM messages a 
LEFT JOIN usersmessages b 
ON b.messageid = a.messageid 
WHERE a.messageid = 1

Translation: Delete from table messages, where messageid = 1, if table uersmessages has messageid = messageid of table messages, then delete the rowUser message table.

P粉595605759

Can't they be separated by semicolons?

Delete from messages where messageid = '1';
Delete from usersmessages where messageid = '1'

or

Just use INNER JOIN as shown below

DELETE messages , usersmessages  FROM messages  INNER JOIN usersmessages  
WHERE messages.messageid= usersmessages.messageid and messages.messageid = '1'
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template