简体   繁体   English

SQL查询内部连接和OR

[英]SQL Query Inner Join and OR

How do i select using INNER JOIN and OR , i tried using below query but it doesn't return anything also no error 我如何使用INNER JOINOR选择,我尝试使用下面的查询,但它不会返回任何错误

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
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

The change here is 2 joins, one join per user on the user table. 此处的更改是2个联接,用户表上的每个用户一个联接。 This is because you have 2 user ids in the message table that are different (no one chats to themselves) so you need a join for userid_a and another join for userid_b . 这是因为消息表中有2个用户ID是不同的(没有人自己聊天)所以你需要一个userid_a的连接和另一个userid_b

The join you had was effectively returning 0 records because userid_a will never equal userid_b . 您所拥有的userid_a实际上返回了0条记录,因为userid_a永远不会等于userid_b Also you do not want an OR in your WHERE clause for this query. 此外,您不希望WHERE子句中的OR用于查询。

The WHERE clause was fine. WHERE子句没问题。

Igor 's answer is the straightforward way, but I am thinking of getting rid of OR which is usually associated with poorer performance and UNION the results for each reg_userid . Igor的答案是直截了当的方式,但我想要摆脱通常与较差的表现相关的OR ,以及UNION每个reg_userid的结果。 Something like the following: 类似于以下内容:

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

Although this might be faster, the data for a specific chatroom_message is slightly harder to get, as it is split between 2 records. 虽然这可能更快,但特定chatroom_message的数据稍微难以获得,因为它分为2个记录。 It depends on what you are doing with this data afterwards. 这取决于你之后对这些数据的处理方式。

您需要两个内连接才能工作 - 一个用于userid_a,另一个用于userid_b。

Let's say :chat_client = 123 . 让我们说:chat_client = 123 Then you'd select all chatroom_message records where userid_a = 123 plus all chatroom_message records where userid_b = 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

To these records you join users_account . 对于这些记录,您加入users_account This gets you: 这会让你:

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

So if you don't get any records, then there simply is no chatroom_message record for user 123 (or there is no matching entry in users_account which should be impossible due to foreign key constraints. 因此,如果您没有获得任何记录,那么用户123就没有chatroom_message记录(或者users_account没有匹配的条目,由于外键约束,这是不可能的。

The query is fine. 查询没问题。 Check your parameter and data. 检查您的参数和数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM