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 7944801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:39:40+00:00 2026-06-04T00:39:40+00:00

I have a usermessages table in which all messages sent between two users on

  • 0

I have a usermessages table in which all messages sent between two users on a site are stored.

Each message has a sourceUserId and a friendId, these are the sender and receiver.

Every user message inbox lists all sent and received messages and each message shows which user has sent or received that message.

The problem

As we are listing sending and receiving users, some columns are the wrong way around, for example if the userId of the current user is 1

  • sent messages will have a sourceUserId of 1
  • received messages will have a friendId of 1

If I try and do the whole inbox with one query then I have to currently do two joins on both sourceUserId and friendId as I can’t tell which is the currently logged in user.

I already know the username of the currently logged in user so it does not seem right to have two joins when only one is needed?

I am wondering if it is possible to use some sort of case in a mysql query to effectively achieve the following and reduce the amount of data that has to be looked up each time ..

SELECT um.message, UNIX_TIMESTAMP(um.time), um.read, user.username
FROM usermessages um
    CASE 
        WHEN um.friendId = 1
            INNER JOIN user ON um.sourceUserId = user.id
        WHEN um.sourceUserId = 1        
            INNER JOIN user ON um.friendId = user.id
    END
WHERE (um.friendId = 1 OR um.sourceUserId = 1)
  • 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-06-04T00:39:41+00:00Added an answer on June 4, 2026 at 12:39 am

    You need to use CASE for ON clause instead

    SELECT um.message, UNIX_TIMESTAMP(um.time), um.read, user.username
    FROM usermessages um
    INNER JOIN user ON 
        CASE 
            WHEN um.friendId = 1 THEN um.sourceUserId
                                 ELSE um.friendId
        END = user.id
    WHERE (um.friendId = 1 OR um.sourceUserId = 1)
    

    (or move the comparison = user.id inside CASE if you like it better)

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

Sidebar

Related Questions

I have 3 tables, each table will show records of users posting messages. I
i have this form_for sending a private message between two users on my users#show
I have to tables:users and messages.And create the one-many relations between these tables.And,If I
Message has_many user_messages. It has two. 1 UserMessage will have the sender as user_id
I have two tables in MySQL #messages table : messageid messagetitle . . #usersmessages
In my messages_controller I have the following private method: def find_message_or_404(slug) message = user.messages.find_by_slug(slug)
I have a script with many debug messages, which are printed by PRINT function.
I have two directories of text files. One has the User Description and the
i have <li><%= link_to Messages, user_messages_path %></li> in my routes.rb i have resources :users
This sounds simple, and probably is... Alright, so I have two tables, users and

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.