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

  • Home
  • SEARCH
  • 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 8507969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:01:23+00:00 2026-06-11T03:01:23+00:00

i have a table called users contained (Table A) Users user_id username user_type[1,2] (Table

  • 0

i have a table called users contained

(Table A) 
Users
user_id
username
user_type[1,2]

(Table B)     if user_type=1       
user_id
full_name

(Table C)     if user_type=2        
user_id 
full_name

i want to get single record set by executing single query, is that possible in PHP mysql.

  • 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-11T03:01:25+00:00Added an answer on June 11, 2026 at 3:01 am

    Try this:

    SELECT table_a.*, COALESCE(table_b.full_name,table_c.full_name) AS full_name
    FROM table_a
    LEFT OUTER JOIN table_b ON table_b.user_id = table_a.user_id
    LEFT OUTER JOIN table_c ON table_c.user_id = table_a.user_id WHERE 1;
    

    It uses the LEFT OUTER JOIN, which means that it joins it to table_b on the given condition. However, for each row in table_a, whether it finds a matching row in table_b or not, it will return the table_a row. If it does not find a matching row, the table_b columns are just NULL. Same thing with table_c.

    Then, we just select all the table_a columns. However, we now have two full_name columns, one from table_b and one from table_c. We use COALESCE to merge them.

    COALESCE returns the first non-NULL value.

    Since we know that there is either a matching row in table_b or a matching row in table_c, it is not a problem. However, it would be a problem if somehow you allows a matching row to be found in both table_b and table_c.

    The risk can be mitigated by adding additional ON clause conditions to get:

    SELECT table_a.*, COALESCE(table_b.full_name,table_c.full_name) AS full_name
    FROM table_a
    LEFT OUTER JOIN table_b
                 ON table_b.user_id = table_a.user_id AND table_a.user_type = 1
    LEFT OUTER JOIN table_c
                 ON table_c.user_id = table_a.user_id AND table_a.user_type = 2
    WHERE 1;
    

    Still, you will need to make sure only 1 row is present for each user in table_b and table_c.


    Instead of COALESCE you can optionally use CASE like:

    SELECT table_a.*, CASE user_type WHEN 1
            THEN table_b.full_name
            ELSE table_c.full_name END AS full_name
    ...
    

    or use an IF function like:
    SELECT table_a.*, IF(user_type=1,table_b.full_name,table_c.full_name) AS full_name
    …

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

Sidebar

Related Questions

I have a table called order which contains columns id , user_id , price
Say I have a table called Users, which contains your typical information: Id, Name,
I have a table called Users ( class User < ActiveRecord::Base ) and a
Let's say I have a table called Users which represents registered users of a
I have a MySQL table called Users. The rows have 4 fields and look
I have a table called UserPermissions with a FK to the users table by
I have a table, called 'files', where I store file_id's. Basically, users on my
I have a table called credit_log and the columns are user_id | amount The
I have a table called USERS that has a foreign key to the table
I have a table called changes which contains a list of record numbers the

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.