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 @ 2022-01-17 16:28  了悟  阅读(905)  评论(0编辑  收藏  举报