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