SQL Query Inner Join and OR

How can I choose with INNER JOINand OR, I tried to use the query below, but it returns nothing and errors

SELECT * FROM chatroom_message cm
INNER JOIN users_account ua
ON cm.userid_a = ua.reg_userid 
OR cm.userid_b = ua.reg_userid 
WHERE cm.userid_a = :chat_client OR cm.userid_b = :chat_client
+4
source share
4 answers
SELECT * 
FROM chatroom_message cm INNER JOIN users_account ua ON cm.userid_a = ua.reg_userid 
INNER JOIN users_account ub ON cm.userid_b = ub.reg_userid 
WHERE cm.userid_a = :chat_client OR cm.userid_b = :chat_client

Here 2 connections are changed, one connection for each user in the user table. This is because you have 2 user IDs in the message table that are different from each other (no one will quote for themselves), so you need a connection for userid_aand another connection for userid_b.

, , 0 , userid_a userid_b. OR WHERE .

WHERE .

+3

Igor - , OR, , UNION - reg_userid. - :

SELECT * FROM chatroom_message cm
INNER JOIN users_account ua ON cm.userid_a = ua.reg_userid 
WHERE cm.userid_a = :chat_client
UNION ALL
SELECT * FROM chatroom_message cm
INNER JOIN users_account ua ON cm.userid_b = ua.reg_userid 
WHERE cm.userid_b = :chat_client

, chatroom_message , . , .

+3

: userid_a userid_b.

0

:chat_client = 123. chatroom_message, userid_a = 123 chatroom_message , userid_b = 123.

userid_a  userid_b
111       222         <- this one not
333       123         <- this one yes
123       444         <- this one yes

users_account. :

userid_a  userid_b  username
333       123       Mr. 333
333       123       Mr. 123
123       444       Mr. 123
123       444       Mr. 444

, , 123 chatroom_message ( users_account, - .

. .

0

Source: https://habr.com/ru/post/1687330/


All Articles