left join 连接之后 where条件字段为空 解决办法

可以left join 之后加and  条件。这样查询出来的记录不会少,为空的也能查出来,但是仍然能筛选

 

 

 

例子:

select t.product_id,
t.unit_price as price,
t.number,
t.item_total_price,
t.remark,
t.im_cost_price,
t.im_cost_price_intax,
t.create_user_id,
t.item_currency,
p.sku,
p.belong_stock,
p.cost_price,
o.order_no,
s.product_address as photoUrl,
s.show_kind,
s.is_main_photo
from trade_order_item t left join trade_product_v2 p on t.product_id = p.product_id left join trade_product_show s on t.product_id = s.product_id
and s.is_main_photo = 0

这里trade_product_show

这里表的is_main_photo 字段不是所有记录都有值,所以不能在where子句上筛选 所以 left join 之后加and

select t.product_id,
t.unit_price as price,
t.number,
t.item_total_price,
t.remark,
t.im_cost_price,
t.im_cost_price_intax,
t.create_user_id,
t.item_currency,
p.sku,
p.belong_stock,
p.cost_price,
o.order_no,
s.product_address as photoUrl,
s.show_kind,
s.is_main_photo
from trade_order_item t left join trade_product_v2 p on t.product_id = p.product_id left join trade_product_show s on t.product_id = s.product_id
and s.is_main_photo = 0
posted @   了悟  阅读(964)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2020-01-17 git 提交部分修改的文件,以及如何撤回已经add的文件
点击右上角即可分享
微信分享提示