0

supplier:供应商
dealer:经销商

select * from table
where
    if(supplier!=0){            //当供应商不为0时,goods_id使用供应商的goods_id 
        goods_id=supplier_goods
    }
    else(dealer!=0){           //当经销商不为0时,goods_id使用经销商的goods_id      
        goods_id=dealer_goods 
    }
    

可以有这种写法吗? tp中又该怎么写?

查看全部 7 个回答

0

已采纳

//大概这样,只是个大概:
if($supplier !==0 ){ //当供应商不为0时,goods_id使用供应商的goods_id

    $where = 'goods_id=supplier_goods';

}else($dealer !== 0){ //当经销商不为0时,goods_id使用经销商的goods_id

    $where = 'goods_id=dealer_goods'; 

}
$data = $mysql->query("select * from table where {$where}");
建议将判断逻辑放在前面,让SQL一目了然

推荐答案

1

$where['goods_id'] = ($supplier != 0) ? $supplier_goods : $dealer_goods;

Db::table('table')->where($where)->select();