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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM