【问题标题】:How to trace out values from three different tables sql JOIN?如何从三个不同的表 sql JOIN 中追踪值?
【发布时间】:2013-04-09 18:24:39
【问题描述】:

我有 3 个如下表

table_1

securityno name    price
1           a11    12.12
2           z11    44.4

table_2

name      identifier Mainprice
a11_bond  NO34         11
z22_bond  NO98         22



table_3
securityno name    identifier 
1           a11    NO34         
2           z11    NO98         

我想检查table_1 的价格是否与table_2 一致 我只想显示来自table_2 的输出table_1 数据和Mainprice

securityno name    price Mainprice
1           a11    12.12 11
2           z11    44.4  22

我在尝试

select * from table_1 left join table_2 on table_3呢?

未能使用 3 个表。

请帮忙。

【问题讨论】:

    标签: sql sql-server tsql join


    【解决方案1】:

    试试:

    SELECT  
        t1.*,  
        t2.Mainprice  
    FROM table_1 AS t1  
    LEFT JOIN table_3 AS t3  
       ON t1.securityno = t3.securityno AND t1.name = t3.name  
    INNER JOIN table_2 AS t2  
       ON t2.identifier = t3.identifier  
    

    【讨论】:

    • FROM `table_1` 中的所有值,所以需要使用LEFT JOIN 如果有`t2.identifier = t3.identifier`identifier 与来自table_1 的公司名称不匹配,也需要显示??
    • 尝试更改的代码,让我知道它是否有效。如果您可以为您在问题中提到的案例添加示例数据,将会很有帮助
    【解决方案2】:

    简单使用INNER JOIN

    SELECT T1.*,
           T2.mainprice
    FROM TABLE1 T1
    INNER JOIN Table3 T3 ON T3.securityno = T1.securityno
    INNER JOIN Table2 T2 ON T2.identifier = T3.identifier
    

    DEMO

    【讨论】:

    • FROM `table_1` 中的所有值,所以需要使用LEFT JOIN 如果有`t2.identifier = t3.identifier`identifier 与来自table_1 的公司名称不匹配,也需要显示??
    猜你喜欢
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多