首页 > 解决方案 > 将 3 个具有相同日期的表的结果连接起来

问题描述

我有3个这样的表:

表格1

ID  Date        Number   City
--------------------------------
1   18.11.2019  345      Bristol

表2

ID  Date        Type       Code
-------------------------------
3   18.11.2019  returned   11

表3

ID  Date        Source    Product
----------------------------------
39  18.11.2019  unknown   shirt

我想编写一个查询,在所有三个表上显示同一日期的结果。

期望的结果:

Date        table1_Number   table1_City  table2_Type   table2_Code  table3_Source  table3_Product
18.11.2019  345             Bristol      returned      11           unknown        shirt

标签: sql

解决方案


您是否正在寻找简单的连接,如下所示?

select 
    t1.number  table1_number,
    t1.city    table1_city,
    t2.type    table2_type,
    t2.code    table2_code,
    t3.source  table3_source,
    t3.product table3_product
from table1 t1
inner join table2 t2 on t1.date = t.date
inner join table3 t3 on t3.date = t.date

推荐阅读