I have a chat user entry in a MySql table –
id (primary key, autoincrement) – the chat user id
user_id – the users id as it pertains to our product
room_id – the id of the room your in
If a chat operator enters the room, leaves, and then comes back – as it is now – will create two entries in the room (INSERT INTO chatuser …)
I know INSERT IF NOT EXISTS syntax, however I want to IF NOT EXISTS not on the primary key, but WHERE user_id = x AND room_id = y (which will be the same if they re-enter a room they have been in)
something like INSERT INTO chatuser SET user_id=4, room_id=2 IF ENTRY DOESNT EXIST WITH THOSE VALUES ALREADY 😉
thank you
If you have a unique index on (user_id,room_id), you can do
INSERT ... ON DUPLICATE KEY UPDATE(orINSERT IGNOREif you don’t need to update anything when the record already exists):