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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:18:08+00:00 2026-06-03T22:18:08+00:00

Im trying to do multiple LEFT JOIN’s on the same column of a table.I

  • 0

Im trying to do multiple LEFT JOIN’s on the same column of a table.I need to LEFT JOIN “table2.words” with “table1.color” and “table2.words” with “table1.food”. How do I do this? and can I do it by making the left joined “table2.words” a new column?

My SQL code:

SELECT table1.id, table1.color, table2.words
FROM table1
LEFT JOIN table2 ON table1.color=table2.id
LEFT JOIN table2 ON table1.food=table2.id 

table1:

--------------------------------
| id     | color    | food     |
--------------------------------
| 1      | 1        | 3        |
| 2      | 1        | 4        |
| 3      | 1        | 3        |
| 4      | 1        | 4        |
| 5      | 2        | 3        |
| 6      | 2        | 4        |
| 7      | 2        | 3        |
| 8      | 2        | 4        |
--------------------------------

table2:

---------------------------
| id     | words          |
---------------------------
| 1      | yellow         |
| 2      | blue           |
| 3      | cookies        |
| 4      | milk           |
---------------------------

What Im trying to output:

----------------------------------------
| id     | colorname    | foodname     |
----------------------------------------
| 1      | yellow       | cookies      |
| 2      | yellow       | milk         |
| 3      | yellow       | cookies      |
| 4      | yellow       | milk         |
| 5      | blue         | cookies      |
| 6      | blue         | milk         |
| 7      | blue         | cookies      |
| 8      | blue         | milk         |
----------------------------------------

Note: I cant change the table structures.

  • 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-03T22:18:09+00:00Added an answer on June 3, 2026 at 10:18 pm
    SELECT
        table1.id,
        table2_A.words colorname,
        table2_B.words foodname
    FROM table1 
    LEFT JOIN table2 table2_A ON table1.color=table2_A.id 
    LEFT JOIN table2 table2_B ON table1.food=table2_B.id;
    

    Your Sample Data

    mysql> drop database if exists supercoolville;
    Query OK, 2 rows affected (0.06 sec)
    
    mysql> create database supercoolville;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> use supercoolville;
    Database changed
    mysql> create table table1
        -> (
        ->     id int not null auto_increment,
        ->     color int,
        ->     food int,
        ->     primary key (id)
        -> );
    Query OK, 0 rows affected (0.06 sec)
    
    mysql> insert into table1 (color,food) values
        -> (1,3),(1,4),(1,3),(1,4),
        -> (2,3),(2,4),(2,3),(2,4);
    Query OK, 8 rows affected (0.06 sec)
    Records: 8  Duplicates: 0  Warnings: 0
    
    mysql> create table table2
        -> (
        ->     id int not null auto_increment,
        ->     words varchar(20),
        ->     primary key (id)
        -> );
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> insert into table2 (words) values
        -> ('yellow'),('blue'),('cookies'),('milk');
    Query OK, 4 rows affected (0.07 sec)
    Records: 4  Duplicates: 0  Warnings: 0
    
    mysql> select * from table1;
    +----+-------+------+
    | id | color | food |
    +----+-------+------+
    |  1 |     1 |    3 |
    |  2 |     1 |    4 |
    |  3 |     1 |    3 |
    |  4 |     1 |    4 |
    |  5 |     2 |    3 |
    |  6 |     2 |    4 |
    |  7 |     2 |    3 |
    |  8 |     2 |    4 |
    +----+-------+------+
    8 rows in set (0.01 sec)
    
    mysql> select * from table2;
    +----+---------+
    | id | words   |
    +----+---------+
    |  1 | yellow  |
    |  2 | blue    |
    |  3 | cookies |
    |  4 | milk    |
    +----+---------+
    4 rows in set (0.00 sec)
    

    Results of My Query

    mysql> SELECT
        ->     table1.id,
        ->     table2_A.words colorname,
        ->     table2_B.words foodname
        -> FROM table1
        ->     LEFT JOIN table2 table2_A ON table1.color=table2_A.id
        ->     LEFT JOIN table2 table2_B ON table1.food=table2_B.id
        -> ;
    +----+-----------+----------+
    | id | colorname | foodname |
    +----+-----------+----------+
    |  1 | yellow    | cookies  |
    |  2 | yellow    | milk     |
    |  3 | yellow    | cookies  |
    |  4 | yellow    | milk     |
    |  5 | blue      | cookies  |
    |  6 | blue      | milk     |
    |  7 | blue      | cookies  |
    |  8 | blue      | milk     |
    +----+-----------+----------+
    8 rows in set (0.00 sec)
    
    mysql>
    

    UPDATE 2012-05-14 19:10 EDT

    In the event there are values for food or color that do not exist, here is the adjusted query:

    SELECT
        table1.id,
        IFNULL(table2_A.words,'Unknown Color') colorname,
        IFNULL(table2_B.words,'Unknown Food') foodname
    FROM table1 
    LEFT JOIN table2 table2_A ON table1.color=table2_A.id 
    LEFT JOIN table2 table2_B ON table1.food=table2_B.id;
    

    I will add rows to table1 and run this new query

    mysql> drop database if exists supercoolville;
    Query OK, 2 rows affected (0.13 sec)
    
    mysql> create database supercoolville;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> use supercoolville;
    Database changed
    mysql> create table table1
        -> (
        ->     id int not null auto_increment,
        ->     color int,
        ->     food int,
        ->     primary key (id)
        -> );
    Query OK, 0 rows affected (0.08 sec)
    
    mysql> insert into table1 (color,food) values
        -> (1,3),(1,4),(1,3),(1,4),
        -> (2,3),(2,4),(2,3),(2,4),
        -> (5,3),(5,4),(2,6),(2,8);
    Query OK, 12 rows affected (0.07 sec)
    Records: 12  Duplicates: 0  Warnings: 0
    
    mysql> create table table2
        -> (
        ->     id int not null auto_increment,
        ->     words varchar(20),
        ->     primary key (id)
        -> );
    Query OK, 0 rows affected (0.08 sec)
    
    mysql> insert into table2 (words) values
        -> ('yellow'),('blue'),('cookies'),('milk');
    Query OK, 4 rows affected (0.06 sec)
    Records: 4  Duplicates: 0  Warnings: 0
    
    mysql> select * from table1;
    +----+-------+------+
    | id | color | food |
    +----+-------+------+
    |  1 |     1 |    3 |
    |  2 |     1 |    4 |
    |  3 |     1 |    3 |
    |  4 |     1 |    4 |
    |  5 |     2 |    3 |
    |  6 |     2 |    4 |
    |  7 |     2 |    3 |
    |  8 |     2 |    4 |
    |  9 |     5 |    3 |
    | 10 |     5 |    4 |
    | 11 |     2 |    6 |
    | 12 |     2 |    8 |
    +----+-------+------+
    12 rows in set (0.00 sec)
    
    mysql> select * from table2;
    +----+---------+
    | id | words   |
    +----+---------+
    |  1 | yellow  |
    |  2 | blue    |
    |  3 | cookies |
    |  4 | milk    |
    +----+---------+
    4 rows in set (0.00 sec)
    
    mysql> SELECT
        ->     table1.id,
        ->     IFNULL(table2_A.words,'Unknown Color') colorname,
        ->     IFNULL(table2_B.words,'Unknown Food') foodname
        -> FROM table1
        -> LEFT JOIN table2 table2_A ON table1.color=table2_A.id
        -> LEFT JOIN table2 table2_B ON table1.food=table2_B.id;
    +----+---------------+--------------+
    | id | colorname     | foodname     |
    +----+---------------+--------------+
    |  1 | yellow        | cookies      |
    |  2 | yellow        | milk         |
    |  3 | yellow        | cookies      |
    |  4 | yellow        | milk         |
    |  5 | blue          | cookies      |
    |  6 | blue          | milk         |
    |  7 | blue          | cookies      |
    |  8 | blue          | milk         |
    |  9 | Unknown Color | cookies      |
    | 10 | Unknown Color | milk         |
    | 11 | blue          | Unknown Food |
    | 12 | blue          | Unknown Food |
    +----+---------------+--------------+
    12 rows in set (0.00 sec)
    
    mysql>
    

    Given any invalid data, LEFT JOIN in still needed.

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

Sidebar

Related Questions

I am trying to JOIN multiple tables to the same value in a table.
I am trying to load multiple elements with the same name from XML into
I am trying to figure out how to use multiple left outer joins to
I'm trying to join multiple tables together but I'm having a bit of trouble
Im trying to select a table with multiple joins, one for the number of
Im probably missing something painfully obvious here. Im trying to join multiple tables together
I'm trying to join multiple tables, but one of the tables has multiple records
Im trying to make multiple quizes that will keep track of each quiz on
I'm trying to add multiple instances of UIImageView(s) to an NSMutableArray of NSMutableDictionar(ies). I
I'm trying to minify multiple CSS files using preg_replace() . Actually, I'm only trying

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.