mysql - Delete rows using JOIN -


i have 2 tables like:

offlineconversations

userid | messageid  100      15 100      16 100      17 

messages

messageid | sentby | message               | convid 15          200      "hi userid 100!"       hash 16          200      "hi again 100!"        hash 17          300      "hi user 300 :)"  hash 

so here, userid 100 received 3 messages, user 200 , 300 (determined sentby in messages table.

what want do, deleted rows in offlineconversations, message sent user id 200, sentby 200 in messages. messageid in offlineconversations table associated messageid in messages offlineconversations left with:

userid | messageid 100      17 

so basically, delete offlineconversations rows userid 100 , sentby (200) associated them.

convid used history aspects. if de-activates account, rows messages should not deleted. if user deletes account entirely, rows should deleted offlineconversations , messages well. 1 know can cascading pointed out in comments.

thanks

this query should work:

 delete t.*   offlineconversations t   join messages m   on m.messageid = t.messageid   m.sentby = 200 , t.userid = 100; 

fiddle

if need delete messages also:

 delete t.*,m.*   offlineconversations t   join messages m   on m.messageid = t.messageid   m.sentby = 200 , t.userid=100; 

fiidle


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -