Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6056565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:23:27+00:00 2026-05-23T08:23:27+00:00

I have following table structure: Users userID (Primary Key), firstName, lastName. Sample data: [1,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:23:28+00:00Added an answer on May 23, 2026 at 8:23 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have an SQL table with basically the following structure: PK (int, primary key),
I have the following table structure: Table Users ID | Name 1 | John
i have the following db structure: Users: - UserID - UserName - FirstName -
I have the table of following structure: UserID StartedOn EndedOn 1 2009-7-12T14:01 2009-7-12T15:01 2
I have the following scenario in nHibernate: <class name=TestApp.Components.User,TestApp.Components table=Users> <id name=Id column=UserId type=Int32
I have the following table structure: Inventory: userid - a - b - c
I have the following database structure (which can't be changed): Users - UserID (int)
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
I have the following table structure (in MySQL): DocID, Code, IsDup, DopOf , where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.