比如需要执行这样一条sql语句

select *
from proofing_notice
where (id between 1 and 10 or id between 50 and 70) and complete = 1 and (title like 'a%' or title like 'b%');

解决方式:

$homeworks = Homework::where(function ($query) {$query->whereBetween('id', [1, 10])->orWhereBetween('id', [50, 70]);
})->where('complete', 1)
->where(function ($query) {$query->where('title', 'like', 'a%')->orWhere('title', 'like', 'b%');
})->get();
DB::table('users')->where(function ($q) {  //闭包返回的条件会包含在括号中return $q->where('id', 3)->orWhere([['name', 'jay'],['age', '>', '18']]);})->where('sex', 1)->get();

sql语句:

select * from `proofing_notice` where `proofing_notice`.`color_id` in (12203, 12204)
and `status` in (9)
and `flag` = 0
and `area_id` in (1, 2)
and (`paper_type_id` = 1 or (`paper_type_id` = 0 and `delivery_date` is not null))
and `proofing_notice`.`deleted_at` is null
order by `updated_at` desc

解决方式:

$grid->model()->with(['proofing_notice'=> function($q1) use($state,$area_id,$type) {$q1->with(['sizegroup','notice_node_new'=>function($qd){$qd->with(['deve_sales_name','deve_aid_name','deve_buyer_name']);}]);$q1->whereIn('status', $state);$q1->where('flag',0);$q1->whereIn('area_id', $area_id);if ($type == 11){$q1->where(function ($q2){$q2->where('paper_type_id',1);$q2->orWhere(function ($q3){$q3->where('paper_type_id',0);$q3->whereNotNull('delivery_date');});});}}])->whereHas("proofing_notice",function($q1) use($state,$area_id,$type) {$q1->whereIn('status', $state);$q1->where('flag',0);$q1->whereIn('area_id', $area_id);if ($type == 11){$q1->where(function ($q2){$q2->where('paper_type_id',1);$q2->orWhere(function ($q3){$q3->where('paper_type_id',0);$q3->whereNotNull('delivery_date');});});}});

sql 语句:

select * from `proofing_notice` where `flag` = 0  and `properties_id` in (2) and exists (select * from deve_style where `proofing_notice`.`exploit_id` = `deve_style`.`id`) and ((`nuclear_date` between '2022-04-01 00:00:00' and '2022-05-01 00:00:00' and `paper_type_id` in (0, 2)) or (`transport_date` between '2022-04-01 00:00:00' and '2022-05-01 00:00:00' and `paper_type_id` in (0, 2))) and `proofing_notice`.`deleted_at` is null

解决方式:

$model = ProofingNotice::with(['exploit' => function($q){$q->with(['client','devedep','deveaids','client_branch','brand']) ;}, 'colors'=>function($q){$q->with(['place']) ;}]);$model = $model->where('flag',0);$model = $model->whereIn('status',$arr_status)->whereIn('properties_id',$arr_properties);if ($type_id == 2){//打样组$model = $model->with(['sample_num'=>function($qn){$qn->where('flag',4);}]) ->whereHas('sample_num', function ($qn) {$qn->where('flag',4);});}else{$model = $model->with(['notice_size']);}$model->whereHas('exploit',function ($q) use ($client_id,$depid,$brand_id,$deve_aid){$q->from(DB::raw( "deve_style"));if(isset($client_id)){$q->where('client_id',$client_id);}if(isset($depid)){$q->whereIn('deve_dep',$depid);}if(isset($brand_id)){$q->where('brand_id',$brand_id);}if(isset($deve_aid)){$q->where('deve_aid',$deve_aid);}});if(isset($place_id)){$model->whereHas('colors',function ($q) use ($place_id){$q->where('place_id',$place_id);});}if($type_id == 2) {$model = $model->whereBetween('quality_date', [$start_date, $end_date]);}else{$model = $model->where(function ($qw) use($start_date,$end_date){$qw->where(function ($qd) use($start_date,$end_date){$qd->whereBetween('nuclear_date', [$start_date, $end_date]);$qd->whereIn('paper_type_id', [0,2]);});$qw->orWhere(function ($qd) use($start_date,$end_date){$qd->whereBetween('transport_date', [$start_date, $end_date]);$qd->whereIn('paper_type_id', [1,3]);});});}if (!Admin::user()->isAdministrator()){$model = $model->whereIn('area_id',$place_from);};$data = $model->get()->toArray();
$map = [['place_work','like','%采购%'],["status","=","1"]];$deveSale = DingEmployee::getSelectApiOptions($map);public static function getSelectApiOptions($where = array()){$options = (new static())->where($where)->select('userid','name')->get();$selectOption = [];foreach ($options as $option){$selectOption[] = ['value' => $option->userid,'label' => $option->name,];}return $selectOption;}

laravel多条件查询方法(and,or嵌套查询)相关推荐

  1. mysql连接与嵌套查询_数据库之嵌套查询与连接查询

    嵌套查询 单值嵌套查询 值返回结果是一个值的嵌套查询称为单值嵌套查询 对Sales数据库,列出市场部的所有员工的编号 USE Sale GO SELECT employee_id FROM emplo ...

  2. Task03:复杂查询方法-视图、子查询、函数等-天池龙珠计划SQL训练营

    Task03:复杂查询方法-视图.子查询.函数等 3.1 视图 3.1.1 什么是视图 3.1.2 视图与表有什么区别 3.1.3 为什么会存在视图 3.1.4 如何创建视图 3.1.5 如何修改视图 ...

  3. Task03:复杂查询方法-视图、子查询、函数等

    Task03:复杂查询方法-视图.子查询.函数等 3.1 视图 3.1.1 什么是视图 3.1.2 视图与表有什么区别 3.1.3 为什么会存在视图 3.1.4 如何创建视图 3.1.5 如何修改视图 ...

  4. 【 Task03:复杂查询方法-视图、子查询、函数等】

    天池龙珠计划Python训练营 文章目录 天池龙珠计划Python训练营 Task03:复杂查询方法-视图.子查询.函数等 3.1 视图 3.1.1 什么是视图 3.1.2 视图与表有什么区别 3.1 ...

  5. 深圳个人档案查询方法(到档查询)

    深圳个人档案查询方法(到档查询) 1. 打开深圳人才网 打开网站下列, 或输入网址 https://www.szhr.com.cn/ 2. 选择到档查询 3. 输出个人信息查询 其他方法 关注公众号也 ...

  6. mysql嵌套查询详解_MySQL嵌套查询实例详解

    本文实例分析了MySQL嵌套查询.分享给大家供大家参考,具体如下: MySQl从4.11版后已经完全支持嵌套查询了,那么下面举些简单的嵌套查询的例子吧(源程序来自MySQL User Manual): ...

  7. mysql嵌套查询详解_MySQL嵌套查询实例详解_MySQL

    本文实例分析了MySQL嵌套查询.分享给大家供大家参考,具体如下: MySQl从4.11版后已经完全支持嵌套查询了,那么下面举些简单的嵌套查询的例子吧(源程序来自MySQL User Manual): ...

  8. 《天池龙珠 - SQL训练营》03.SQL进阶:复杂查询方法-视图、子查询、函数等

    本笔记为阿里云天池龙珠计划SQL训练营的学习内容,链接为:https://tianchi.aliyun.com/specials/promotion/aicampsql 目录 一.视图 1.1 什么是 ...

  9. 阿里云AI训练营-SQL基础3:复杂查询方法-视图、子查询、函数等

    3.1 视图 3.1.1 什么是视图 3.1.2 视图与表有什么区别 3.1.3 为什么会存在视图 3.1.4 如何创建视图 3.1.5 如何修改视图结构 3.1.6 如何更新视图内容 3.1.7 如 ...

最新文章

  1. Android常用URI收藏
  2. fmod实现声音波形_“老张说数理化”—中考物理专题02-声音与环境(收藏可打印)...
  3. 《漫画算法》源码整理-5 排序算法
  4. Eclipse中JSP默认模板修改:设置编码为UTF-8以及导入JSTL等
  5. 利用MySQL创建一个简单的employee员工表并修改表
  6. 算法入门开灯问题,新做法
  7. C# 系统应用之获取IE浏览记录和IE地址栏输入网址
  8. 深圳大学计算机考研复习资料百度云,深圳大学(专业学位)计算机技术研究生考试科目和考研参考书目...
  9. indexed true mysql_一行代码,搞定浏览器数据库 IndexedDB
  10. Confluence 6 重新获得附件指南
  11. IE7IE8不支持rgba的方法
  12. 度量衡计算工具_度量衡计量单位换算转换器
  13. Spring 事务源码(6)—createTransactionIfNecessary处理事务属性并尝试创建事务【两万字】
  14. Android 之路36---android网络操作
  15. MCE | 神经元为胰腺癌细胞提供营养
  16. 压力眼这一Part,久等啦!
  17. 全球及中国硅胶收纳袋行业市场需求动态分析及发展趋势预测报告2022-2028年
  18. HFSS设计双脊喇叭天线之路——天线结构与参数设计
  19. Proxy4Free代理服务器转换为Maxthon(MYIE2)配置
  20. 计算机考研数学基础知识点,2019计算机考研数学7大难点梳理

热门文章

  1. AI会让我们的生活更美好吗?
  2. Python 将网址url转换成二维码
  3. 【区块链之技术进阶】扒一扒某乎上面对于区块链的理解(一)
  4. 转吴小龙的 mvp Android MVP 实例
  5. Web开发的发展史---Web开发技术的演变
  6. 5000字详解KMP算法
  7. MySQL的体系结构
  8. RunJS推荐用于个人使用(使用方便JS、css实时预览、编辑、管理等功能)
  9. 高翔orbslam_ORB-SLAM2
  10. 【Java】编写程序完成一个简易的可以弹奏的钢琴