如何在 SQL 中使用多个 LEFT JOIN?

新手上路,请多包涵

是否可以在 sql 查询中使用多个左连接?

     LEFT JOIN
        ab
    ON
        ab.sht = cd.sht

我想添加一个这样的查询吗?它会起作用吗?

     LEFT JOIN
        ab AND aa
    ON
        ab.sht = cd.sht
           AND
        aa.sht = cc.sht

这行得通吗?

原文由 cute 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 800
1 个回答

您有两种选择,具体取决于您的餐桌顺序

create table aa (sht int)
create table cc (sht int)
create table cd (sht int)
create table ab (sht int)

-- type 1
select * from cd
inner join cc on cd.sht = cc.sht
LEFT JOIN ab ON ab.sht = cd.sht
LEFT JOIN aa ON aa.sht = cc.sht

-- type 2
select * from cc
inner join cc on cd.sht = cc.sht
LEFT JOIN ab
LEFT JOIN aa
ON aa.sht = ab.sht
ON ab.sht = cd.sht

原文由 RichardTheKiwi 发布,翻译遵循 CC BY-SA 2.5 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进