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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:49:44+00:00 2026-06-10T12:49:44+00:00

I have two tables users and *activation_details* Users table has these data. uid is

  • 0

I have two tables users and *activation_details*

Users table has these data. uid is primary key.

uid     |    user_name
______________________
 1     |    John Smith
 2     |    Mary Smith
 3     |    Nancy Smith
 4     |    Agent Smith

activation_details has these data

aid     |    is_activated   |  user_id
______________________________________
1      |         0         |     0
2      |         1         |     4
3      |         1         |     1
4      |         1         |     777 

Please note that user id 777 is not in the users table.

I need the result in this format

aid     |    is_activated   |  user_name      
______________________________________________
1       |         0         |     0
2       |         1         |     Agent Smith
3       |         1         |     John Smith
4       |         1         |     777 

If user id is present in the users table then username has to be displayed, else the user_id itself has to be displayed.

I tried some thing like this

SELECT aid, is_activated, user_id, users.user_name from activation_details, users WHERE users.uid = user_id

It gives this output, which is not useful.

aid     |    is_activated   |  user_name      | user_id
________________________________________________________ 
2       |         1         |     Agent Smith |     4 
3       |         1         |     John Smith  |     1

Is there anyway to get this output using mysql only, I can do it with multiple queries in PHP, but that is not what I am looking.

aid     |    is_activated   |  user_name
______________________________________
1       |         0         |     0
2       |         1         |     Agent Smith
3       |         1         |     John Smith
4       |         1         |     777 
  • 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-10T12:49:45+00:00Added an answer on June 10, 2026 at 12:49 pm

    You want to use an outer join in combination with the coalesce() function (which returns the first non-null value in the selected columns) like this:

    SELECT 
        a.aid, 
        a.is_activated, 
        a.user_id, 
        coalesce(b.user_name, a.user_id, 0) as User
    from 
        activation_details a
            left outer join users b
                on a.user_id=b.uid
    

    Edit: I added one more check for the coalesce() function which will return a 0 for the User in case both the user_name and user_id fields are null values.

    Testing done in mysql:

    mysql> select * from first
        -> ;
    +------+-------+
    | id   | title |
    +------+-------+
    |    1 | aaaa  |
    |    2 | bbbb  |
    |    3 | cccc  |
    +------+-------+
    3 rows in set (0.00 sec)
    
    mysql> select coalesce('a', id) from first
        -> ;
    +-------------------+
    | coalesce('a', id) |
    +-------------------+
    | a                 |
    | a                 |
    | a                 |
    +-------------------+
    3 rows in set (0.00 sec)
    
    mysql> select coalesce('a', 1) from first;
    +------------------+
    | coalesce('a', 1) |
    +------------------+
    | a                |
    | a                |
    | a                |
    +------------------+
    3 rows in set (0.00 sec)
    
    mysql> select coalesce(null, 1) from first;
    +-------------------+
    | coalesce(null, 1) |
    +-------------------+
    |                 1 |
    |                 1 |
    |                 1 |
    +-------------------+
    3 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: Users and Files . Users table has 10 rows, Files
I Have two tables: Users and News. Table News has columns id, name, createdby,
I have two tables as below: users -id (primary key, AUTO_INCREMENT) -username (varchar) -password
I have two tables: users: user_id user_name data: user_id user_data user_time I wan to
suppose we have two tables users and products table users has an accountBalance column
I have two tables- users and language with a foreign key link of their
Hi, I have these two tables: users and friends (friend_status = 1 means the
Hi, I have these two tables: users and friends (friend_status = 1 means the
We have two tables - Tasks and TasksUsers (users assigned to task). Task has
I'm using SQL-Server 2005. I have two tables Users and Payments which has a

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.