1

I have 2 tables that look 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 I am user 300 :)"  hash

So here, UserID 100 received 3 messages, from user 200 and 300 (determined by sentBy in the messages table.

What I want to do, is deleted all rows in offlineconversations, where the message was sent by the user with id 200, so sentBy 200 in messages. The messageID in the offlineconversations table is associated with the messageID in messages Then offlineconversations would only be left with:

userID | messageID
100      17

So basically, delete from offlineconversations the rows where the userID is 100 and sentBy (200) is associated to them.

convID is used for the history aspects. If someone de-activates his account, the rows from messages should not be deleted. If the user deletes his account entirely, then the rows should be deleted from offlineconversations AND messages as well. That one I know I can do from cascading as someone pointed out in the comments.

Thanks

10
  • presumably you have a foreign key relationship between those two tables? a cascading delete would take care of this, e.g. deleting (15/200) in messages would delete (100/15) in offlineconversations as well. Commented Apr 10, 2014 at 21:16
  • The thing is I don't want to delete the actual messages from messages, as I will use it for history if the contacts decide to become friends again. Commented Apr 10, 2014 at 21:17
  • 2
    but if you delete (100/15) from offlineconversations, how could you re-associate that message with user 100? once 100/15 is gone, there'd be no recipient attached to messsage 15 anymore and no way to tell who it was sent to originally. Commented Apr 10, 2014 at 21:19
  • 1
    that'd imply you're re-using IDs, then. This is one reason re-using IDs is a bad idea. A new account should get a new ID, then this problem becomes moot. Commented Apr 10, 2014 at 21:26
  • 2
    but why would you need to change/delete anything in the messages on deactivation then? presumably that'd just be a single flag field in a users table, e.g. deactivated=1. no deleting or anything, just a simple update 0 => 1. Commented Apr 10, 2014 at 21:29

2 Answers 2

4

This query should work:

 DELETE t.* 
 FROM offlineconversations t 
 JOIN messages m 
 ON m.messageID = t.messageID 
 WHERE m.sentBy = 200 AND t.userID = 100;

Fiddle

If you need to delete messages also:

 DELETE t.*,m.* 
 FROM offlineconversations t 
 JOIN messages m 
 ON m.messageID = t.messageID 
 WHERE m.sentBy = 200 AND t.userID=100;

Fiidle

Sign up to request clarification or add additional context in comments.

7 Comments

Let's say that user 200 also sent to users 101, 102, 103 etc. Will it not delete the rows associated with these other contacts as well?
you need Foreign keys for this, or DELETE t.*, m.* ... or even DELETE *
Does your query delete all rows sent (to anybody, not just user 100) by user 200 in both tables?
not only in offlineconversations, if you want both, write DELETE *
Err, when I wrote my 'basically' sentence I made a mistake, I should of said: So basically, delete from offlineconversations the rows where the userID is 100 and sentBy (200) is associated to them.
|
3

what about :

    delete from offlineconversations where messageID in (select messageId from 
messages where sentBy = 200)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.