简体   繁体   中英

LEFT JOIN on multiple columns and conditions

I have two tables, one for orders and one for products

ORDERS TABLE 

+----------+-----------+--------+---------------------+----------+----------+---------------------+--------+
| order_id | order_by  | status | created_at          | product1 | product2 | delivery_date       | pickup |
+----------+-----------+--------+---------------------+----------+----------+---------------------+--------+
|        1 | Maria     |      0 | 2020-07-19 00:00:00 |        1 |        3 | 2020-07-17 00:00:00 |      0 |
|        2 | Joao      |      0 | 2020-07-20 00:00:00 |        2 |        0 | 2020-07-20 00:00:00 |      0 |
|        3 | Jose      |      1 | 2020-07-20 00:00:00 |        3 |        0 | 2020-07-20 00:00:00 |      0 |
|        4 | Matias    |      0 | 2020-07-19 23:50:40 |        1 |        3 | 2020-07-19 00:00:00 |      0 |
|        5 | Matias    |      0 | 2020-07-19 23:50:40 |        1 |        3 | 2020-07-19 00:00:00 |      0 |
|        6 | Joaozinho |      0 | 2020-07-19 00:00:00 |        1 |        3 | 2020-07-22 00:00:00 |      0 |
+----------+-----------+--------+---------------------+----------+----------+---------------------+--------+


PRODUCTS TABLE

+----+-----------+-------+
| id | name      | price |
+----+-----------+-------+
|  1 | Produto 1 |    11 |
|  2 | Produto 2 |    22 |
|  3 | Produto 3 |    33 |
|  4 | Produto 4 |    44 |
|  5 | Produto 5 |    55 |
+----+-----------+-------+


EXPECTED OUTPUT

+----------+-----------+--------+---------------------+----------+----------+---------------------+--------+-----------+----------+-----------+----------+
| order_id | order_by  | status | created_at          | product1 | product2 | delivery_date       | pickup | p1_name   | p1_price | p2_name   | p2_price |
+----------+-----------+--------+---------------------+----------+----------+---------------------+--------+-----------+----------+-----------+----------+
|        1 | Maria     |      0 | 2020-07-19 00:00:00 |        1 |        3 | 2020-07-17 00:00:00 |      0 | Produto 1 |       11 | Produto 3 |      33  |
|        2 | Joao      |      0 | 2020-07-20 00:00:00 |        2 |        0 | 2020-07-20 00:00:00 |      0 | Produto 2 |       22 |   null    |     null |
|        3 | Jose      |      1 | 2020-07-20 00:00:00 |        3 |        0 | 2020-07-20 00:00:00 |      0 | Produto 3 |       33 |   null    |     null |
|        4 | Matias    |      0 | 2020-07-19 23:50:40 |        1 |        3 | 2020-07-19 00:00:00 |      0 | Produto 1 |       11 | Produto 3 |       33 |
|        5 | Matias    |      0 | 2020-07-19 23:50:40 |        1 |        3 | 2020-07-19 00:00:00 |      0 | Produto 1 |       11 | Produto 3 |       33 |
|        6 | Joaozinho |      0 | 2020-07-19 00:00:00 |        1 |        3 | 2020-07-22 00:00:00 |      0 | Produto 1 |       11 | Produto 3 |       22 |
+----------+-----------+--------+---------------------+----------+----------+---------------------+--------+------+-----------+-------+------------------+

I need to LEFT JOIN on every product (product1, product2..) at the orders table and price columns with distinct names (product1_name, product2_name..)

select o.*, p1.name, p1.price
from orders o
inner join products p1 on p1.id=o.product1

adding product2 should not be that hard, and is left as an exercise...

You need two join s ont he products table:

select
    o.*,
    p1.name  p1_name,
    p1.price p1_price,
    p2.name  p2_name,
    p2.price p2_price
from orders o
left join products p1 on p1.id = o.product1
left join products p2 on p2.id = o.product2

If product1 is always available, you can turn the first left join to an inner join , which makes the query marginally more efficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM