【问题标题】:presto sql: multiple join with `using` statementpresto sql:使用`using`语句进行​​多重连接
【发布时间】:2019-11-26 23:58:38
【问题描述】:

我有多个表,我像这样加入它们(它们共享相同的密钥)

select *
from user_account_profile
inner join user_asset_profile
using (user_id)
left join user_trading_profile
using (user_id)

我想知道user_id这个key怎么用?,是不是等价于

select *
from user_account_profile t1
inner join user_asset_profile t2
on t1.user_id = t2.user_id
left join user_trading_profile t3
on t1.user_id = t3.user_id

select *
from user_account_profile t1
inner join user_asset_profile t2
on t1.user_id = t2.user_id
left join user_trading_profile t3
on t2.user_id = t3.user_id

或者如果这些查询都是等价的?

【问题讨论】:

    标签: sql presto


    【解决方案1】:

    您的两个版本在功能上是等效的(除了在不使用using 时重复的user_id 列的明显区别)。第一个inner join 要求两个user_id 具有相同的值,因此任一比较都返回相同的结果集。

    如果您有一系列 left joins,那么您将要求该值位于 first 表中,等价于 t1.user_id

    如果你有full joins,那么你就不会知道。匹配将来自行上具有值的表。如果两个表都有值,则逻辑表示它们相同,因此没有区别。

    【讨论】:

    • ON a.id = b.idUSING (id) 之间存在细微差别。在后一种情况下,结果关系中只有一个 id 列,您不需要(也不能)使用表/别名来限定 id
    猜你喜欢
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 2020-12-06
    相关资源
    最近更新 更多