如何通过sql获取以下数据

fnatzsnv  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(368)

我一直在努力获取以下数据。
我有两张table:

|  Company Name  | Customer ID 1 | Customer ID 2 | Customer ID 3
 +----------------+---------------+---------------+-----------------
 |    Android     |       1       |       2       |      3
 |      IOS       |       4       |       5       |      6

以及下表

|  Customer ID | Customer Name |
 +--------------+---------------+
 |      1       |     Edwin     |     
 |      2       |     Stanley   |       
 |      3       |     Roward    |        
 |      4       |      Kim      |      
 |      5       |     Flare     |       
 |      6       |     Queen     |

如何在sql查询中得到这样的结果?

|  Company Name  | Customer Name1 | Customer Name2 | Customer Name3
 +----------------+----------------+----------------+---------------
 |    Android     |     Edwin      |    Stanley     |      Roward
 |      IOS       |       Kim      |     Flare      |      Queen
ep6jt1vc

ep6jt1vc1#

你可以加入,加入。。。并加入:

select
    t.company_name,
    c1.customer_name customer_name1,
    c2.customer_name customer_name2,
    c3.customer_name customer_name3
from mytable t
inner join customers c1 on c1.customer_id = t.customer_id1
inner join customers c2 on c2.customer_id = t.customer_id2
inner join customers c3 on c3.customer_id = t.customer_id3

这里一件重要的事情是为每个联接使用不同的表别名,以避免多次联接的表上发生冲突。

相关问题