开发者

SQL complex condition

开发者 https://www.devze.com 2023-03-14 08:41 出处:网络
I have following table structure: Users userID (Primary Key), firstName, lastName. Sample data: [1, John, Smith]

I have following table structure:

Users

userID (Primary Key),
firstName,
lastName.

Sample data:
[1, John, Smith]
[2, Steve, Lan]
[3, Matt, Smith] 

Message

messageID (Primary Key),
sender_userID,
receiver_userID,
messageBody,
message-visibile

message-visibile can 0 (Public) or 1 (Private)

Sample data:

[messageID=1, sender_userID=1, receiver_userID=1, messageBody=Hello, message-visibile=1]
[2, 1, 2, Second Message, 0]
[3, 2, 1, ThirdMessage, 1]
[4, 2, receiver_userID=1, FourthMessage, 0]
[5, 3, 3, LastMessage, 0]

Now, I need to display all messages posted/received by a user ID and his colleagues based on message_visible value (0/1). In above sample data, userID 1 is a colleague of userID 2. So, if I query for userID = 1 with message_visible=1, then I should get message ID 1, 3 only. MessageID = 5 should not appear because 3 is not a colleague of any one.

I have tried following query but its not returning expected output:

select 
    mes.MessageId, 
    usr1.sender_userID SenderUserId, 
    usr1.firstName SenderFirstName, 
    usr1.lastName SenderLastName, 
    usr2.userID ReceiverUserId, 
    usr2.firstName ReceiverFirstName, 
    usr2.lastName ReceiverLastName, 
    mes.messageBody
from Message mes
    join Users usr1 on
        mes.sender_userID = usr1.userId
    join Users usr2 on
        mes.receiver_userID = usr2.userId
where 
    mes.sender_userID = 1 and mes.receiver_userID in (1,2) or 
    mes.receiver_userID = 1 and mes.sender_userID in (1,2)
    AND mes.message-visibile = 1

Also, in another below condition, its not working

where 
    mes.sender_userID in (1,2) or 
    mes.receiver_userID in (1,2)

I'll be passing a set of 开发者_运维百科colleagues ID. Am I missing something?


I think, you are missing parenthesis around OR condition:

where 
    mes.message-visibile = 1 and
        (mes.sender_userID = 1 and mes.receiver_userID in (1,2) or 
        mes.receiver_userID = 1 and mes.sender_userID in (1,2))

-- or

where 
    mes.message-visibile = 1 and
        (mes.sender_userID in (1,2) or 
        mes.receiver_userID in (1,2))

If condition is A or B and C it is equivalent to A or (B and C), because and has higher operation precedence. I believe you need (A or B) and C.


Your two conditions look like:

mes.sender_userID = 1 and mes.receiver_userID in (1,2)

OR

mes.receiver_userID = 1 and mes.sender_userID in (1,2)

With the following ALWAYS true:

AND mes.message-visibile = 1

So, you'd want to wrap parenthesis around the two cases above. Alex got close.

select 
    mes.MessageId, 
    usr1.sender_userID SenderUserId, 
    usr1.firstName SenderFirstName, 
    usr1.lastName SenderLastName, 
    usr2.userID ReceiverUserId, 
    usr2.firstName ReceiverFirstName, 
    usr2.lastName ReceiverLastName, 
    mes.messageBody
from Message mes
    join Users usr1 on
        mes.sender_userID = usr1.userId
    join Users usr2 on
        mes.receiver_userID = usr2.userId
where 
    (mes.sender_userID = 1 and mes.receiver_userID in (1,2))
    OR
    (mes.receiver_userID = 1 and mes.sender_userID in (1,2))
    AND mes.message-visibile = 1
0

精彩评论

暂无评论...
验证码 换一张
取 消