You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SELECT语句中通过LEFT OUTER JOIN添加可选关联列input_channel

解决方案

你可以通过两次LEFT OUTER JOIN来实现需求,既关联中间表expression,又能保证无匹配时input_channel返回NULL,同时不会引入expression的字段:

SELECT 
    i.type, 
    i.function, 
    i.enum, 
    ds.input_channel
FROM indicator i
LEFT OUTER JOIN expression e 
    ON i.enum = e.channel
LEFT OUTER JOIN device_serial ds 
    ON ds.device_id = e.shoe_id;

逻辑说明

  • 第一次LEFT JOIN:将indicator表与expression表通过indicator.enum = expression.channel关联,左连接确保即使没有匹配的expression记录,indicator的原有数据也会完整保留。
  • 第二次LEFT JOIN:将关联后的结果与device_serial表通过device_serial.device_id = expression.shoe_id关联,同样用左连接保证当没有匹配的device_serial记录时,input_channel自动返回NULL。
  • 最终仅选取indicator的原有字段和device_serial的input_channel,完全满足你不需要expression字段的要求。

内容的提问来源于stack exchange,提问作者Cheetaiean

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 03:55:26