mysql where后面追加if条件
SELECT
s.status AS zkStatus,
mat.id,
mat.oc_id,
mat.old_owner_code,
mat.old_owner_name,
mat.new_owner_code,
mat.new_owner_name,
mat.contract_no,
mat.mat_code,
mat.mat_version,
mat.ext1,
mat.ext2,
mat.ext3,
mat.ext4,
mat.state,
mat.created_dt,
mat.created_by,
mat.updated_dt,
mat.updated_by,
mat.version,
c.prod_code,
c.prod_name,
c.steel_no,
c.pack_no,
c.grade_no,
c.spec,
c.heat_no,
c.quality,
c.vendor_code,
e.comp_name vendorName,
c.prod_date,
c.face_quality,
c.piece,
c.net_weight,
c.gross_weight,
c.sale_contract_no,
s.location,
mat.settle_cust_code,
mat.settle_cust_name
FROM
tpl_sm_owner_change_mat mat
LEFT JOIN tpl_md_material_v c
ON mat.mat_code = c.mat_code
AND mat.mat_version = c.version
LEFT JOIN tpl_sm_owner_change soc
ON soc.oc_id = mat.oc_id
LEFT JOIN tpl_sm_stock s
ON s.mat_code = c.mat_code
AND s.in_type <> 2
AND soc.sys_code = s.sys_code
AND s.store_code = soc.store_code
LEFT JOIN tpl_md_company e
ON c.vendor_code = e.comp_code
WHERE soc.oc_id IN (
'200526103125'
)
//如果soc.status等于2021,那么就按照s.status排序。反之这倒叙排序
ORDER BY IF(soc.status = 2021,'s.status','s.status desc')