php高级查询,高级查询 · PHP开发知识储备 · 看云

这篇博客探讨了如何使用PHP构造复杂SQL查询,通过$where1、$where2和$where3设置不同条件组合,分别对应待收货、待发货和待支付的订单状态。最终通过whereOr方法将这些条件并行查询,计算出符合条件的订单总金额。
摘要由CSDN通过智能技术生成

多个条件并行查询

~~~

//已完成

$where1 = [

'user_id' => ['in',$childUserIdArr],

'prom_type' => ['

'order_status' => 4

];

//待收货

$where2 = [

'user_id' => ['in',$childUserIdArr],

'prom_type' => ['

'order_status' => 1 ,

'shipping_status' => 1

];

//待发货

$where3 = [

'user_id' => ['in',$childUserIdArr],

'prom_type' => ['

'order_status' => ['in',[0,1]] ,

'shipping_status' => ['neq',1] ,

'pay_status' => 1 ,

];

$totalAmount = $orderModel

->whereOr(function ($query) use ($where1){

$query->where($where1);

})

->whereOr(function ($query) use ($where2){

$query->where($where2);

})

->whereOr(function ($query) use ($where3){

$query->where($where3);

})

->sum('total_amount');

生成的sql:SELECT * FROM `tp_order`

WHERE ( `user_id` IN (4,3,2,11,10,9,8,7,6,5) AND `prom_type` < 5 AND `order_status` = 4 )

OR ( `user_id` IN (4,3,2,11,10,9,8,7,6,5) AND `prom_type` < 5 AND `order_status` = 1 AND `shipping_status` = 1 )

OR ( `user_id` IN (4,3,2,11,10,9,8,7,6,5) AND `prom_type` < 5 AND `order_status` IN (0,1) AND `shipping_status` <> 1 AND `pay_status` = 1 )

~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值