其他分享
首页 > 其他分享> > left join 连接之后 where条件字段为空 解决办法

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

标签:product,join,price,trade,where,id,left
来源: https://www.cnblogs.com/onlyzhangmeng/p/15814041.html