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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:00:47+00:00 2026-06-12T10:00:47+00:00

The question I’ve got here may look silly, but it drives me a bit

  • 0

The question I’ve got here may look silly, but it drives me a bit crazy, as I can’t get how the MySQL join works.

I have 2 tables:

user;
+--------------------+--------------+------+-----+---------+-------+
| Field              | Type         | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| id                 | int(11)      | NO   | PRI | NULL    |       |
| name               | varchar(128) | NO   |     | NULL    |       |
+--------------------+--------------+------+-----+---------+-------+

game;
+--------------------+--------------+------+-----+---------+-------+
| Field              | Type         | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| id                 | int(11)      | NO   | PRI | NULL    |       |
| source_id*         | int(11)      | NO   |     | NULL    |       |
| target_id*         | int(11)      | NO   |     | NULL    |       |
+--------------------+--------------+------+-----+---------+-------+

game.source_id and game.target_id are constraints that reference the user.id.

Now I want to select everything from game table and I want to include in the result names of 2 users, since game has 2 users. And I want those columns to be named as target_name and source_name specifically.

Tried this query:

SELECT
    g.id,
    u.name
FROM
    game g
RIGHT JOIN
    user u
ON
    u.id = g.source_id 
OR
    u.id = g.target_id
WHERE
    g.source_id = 1
OR
    g.target_id = 1
;

But as you may have noticed, I don’t alias u.name as target_name or source_name because I don’t know how to do that and end result looks like this:

+----+------+
| id | name |
+----+------+
| 1  | bo   |
| 1  | al   |
| 2  | jo   |
| 2  | jay  |
+----+------+

As you can see, there’s a game appearing multiple times in the result.

Expected result:

+----+-------------+-------------+
| id | target_name | source_name |
+----+-------------+-------------+
| 1  | bo          | al          |
| 2  | jo          | jay         |
+----+-------------+-------------+

Thanks in advance.

  • 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-12T10:00:48+00:00Added an answer on June 12, 2026 at 10:00 am

    you need to join the table user twice on table game in order to get the two names for the game. I have added COALESCE because it is useful for for showing descriptive value for NULL columns.If for instance there is no value for source or target, the value that will be shown is `-no value-.

    SELECT  a.*,
            COALESCE(b.name,'-no source-') sourceName,
            COALESCE(c.name,'-no target-') targetName
    FROM    game a
            LEFT JOIN `user` b
                on b.id = a.source_ID
            LEFT JOIN `user` c
                on on c.id = a.target_id
    -- your condition here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question: How can I give a new user almost all privileges, but still keep
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Question How can I get this sequence to fire when a user takes action
Question is already been asked in title. Here is a code: (function($){ var filter
Question is similar to this one How to write a MySQL query that returns
Question How can I remove empty xml tags in PHP? Example: $value1 = 2;
Question: Can I override default functions in Javascript? Background: After figuring out that I
Question Is it possible to create a second Preference Dialog that can take some
This could be a duplicate question, but I have no idea what search terms
Question I'm creating a service for my app, but there's probably pritty simple thing

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.