I have a table called last_msg
, in there i store the last mensage from a private chat between two users, and i update the column from
and to
when I send a new mensage. I use this table to show a list of mensages like facebook. I also use this table to another things, so i would rather fix the problem described as bellow.
Because of the ON users.user_id = last_msg.from
i get data only from who is sending the mensage, this was the best i got... This is my current sql:
SELECT `last_msg`.`msg`, `last_msg`.`from`, `users`.`username`, `users`.`avatar`
FROM `last_msg`
INNER JOIN `users`
ON `users`.`user_id` = `last_msg`.`from`
WHERE `last_msg`.`to` = :user_id_logged OR `last_msg`.`from` = :user_id_logged_2
On the INNER JOIN users
I want to get data only from the other user that i'm talking to in the chat, and the data from last_msg
can be from both sender and receiver, as the facebook does.
So i tried:
SELECT `last_msg`.`msg`, `last_msg`.`from`, `users`.`username`, `users`.`avatar`
FROM `last_msg`
INNER JOIN `users`
ON `users`.`user_id` != :user_logged
WHERE (`last_msg`.`to` = :user_logged_2 OR `last_msg`.`from` = :user_logged_3)
But it did not work, it's returning a list of all users in the table users
. Any suggestions about how can i fix it?