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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:12:31+00:00 2026-05-24T08:12:31+00:00

I have MYSQL tables as follows user TABLE {id INT} profile TABLE {user_id INT,

  • 0

I have MYSQL tables as follows

user TABLE {id INT}

profile TABLE {user_id INT, facebook_id varchar(50)}

messages TABLE {id INT, message TEXT, from_id INT, type enum('main','facebook'}

messages_to TABLE {user_id varchar(50), message_id INT}

profile.user_id REFERS To user.id
- the messages_to.message_id refers to the messages.ID column.
- the messages_to.user_id refers to profile.user_id IF messages.type = 'main' BUT
  if message_type = 'facebook' THEN messages_to.user_id REFERS to profile.facebook_id

I want to do a join query that basically selects all the messages to a specific person, but the thing is the messages_to.user_id can refer to either the person's facebook ID or the person's ID (a reference to user.id column).

So basically the query should work as follows

  • it should select all the messages in messages table, and if messages.type = 'facebook' checks if messages_to.user_id equals the person’s FACEBOOK ID. (note that messages_to table stores the recipients for each messages ID)
  • BUT if the messages.type = 'main' checks if the messages_to.user_id equals the person's USER ID (USER.id)

Is it possible to do a mysql join query for that efficiently?

messages_tos table stores ALL the recipients for each message in the MESSAGES table. THERE CAN be MORE THAN ONE RECIPIENT for a message.

  • 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-24T08:12:32+00:00Added an answer on May 24, 2026 at 8:12 am

    I guess this is the query.

    SELECT messages.*,profile.* 
    FROM messages 
    JOIN messages_to ON messages.id = messages_to.message_id
    JOIN profile ON 
      (profile.user_id = messages_to.user_id AND messages.type = 'main') 
      OR (profile.facebook_id = messages_to.user_id AND messages.type = 'facebook')  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a table as follows in MySQL: create table user(name varchar(20), join_time
I have the following tables in MySQL: users profile rates In the users table,
I have two tables in my MySQL database, users and tweets, as follows: TABLE
I'm using PHP/MySql. If I have two tables (hypothetically) as follows: id name and
I have two MySQL tables, and I want to find and replace text strings
I have two MySQL tables: stats (left) and messages (right) +------------+---------+ +---------+------------+-----------+----------+ | _date
I have a MySQL query that goes as follows SELECT count(`clicks`.`user_id`) as total, `users`.`fullname`
In Drupal, I have a table node that gets generated from mysql user data.
I have a MySQL table as follow: id, user, culprit, reason, status, ts_register, ts_update
I have two tables, users and followers : Table users : id INT, PRIMARY_KEY

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.