laravel常用代码库:where条件查询闭包-具有多个AND OR WHERE条件的复杂MySQL查询

条件查询闭包

PHP 闭包的用法:

$query->where(function ($query) use ($keywords) {

    $query->where('items'  , 'like', $keywords)

        ->orwhere('content', 'like', $keywords);

});

laravel 查询闭包


if (isset($priority)) {

                $query->where("priority", $priority);

            }

            // 把名字搜索下面几个条件放在大括号里 不能与 priority 条件 同级别 会影响

            // 如果不放 就这样。

            // select * from `trials` where (`priority` = 0 and `name` like '%肺癌%'or `diseases` like '%肺癌%') 

            // and `trials`.`deleted_at` is null order by `created_at` desc limit 100000 offset 0

            // 放了之后

            // select * from `trials` where `priority` = ? and (`name` like ? or `diseases` like ? or `code` like ?) 

            // and `trials`.`deleted_at` is null order by `created_at` desc limit 100000 offset 0

            if (!empty($name)) {

                $query->where(function ($query) use ($name) {

                    $query->where("name", 'like', "%$name%")

                    ->orwhere("name_abbr", 'like', "%$name%")

                    ->orwhere("name_abbr_out", 'like', "%$name%")

                    ->orwhere("diseases", 'like', "%$name%")

                    ->orwhere("code", 'like', "%$name%");           

                });

            }

此时,这里的sql 查询,priority 这个条件 将会与下面大段条件分开.
sql 语句如下:


select * from `trials` where `priority` = ? and (`name` like ? or `diseases` like ? or `code` like ?) and `trials`.`deleted_at` is null order by `created_at` desc limit 100000 offset 0

运行结果:
laravel常用代码库:where条件查询闭包-具有多个AND OR WHERE条件的复杂MySQL查询

解惑试验-不闭包的sql

我们这样子写 query

if (!empty($name)) {

                $query->where("name", 'like', "%$name%")

                ->orwhere("name_abbr", 'like', "%$name%")

                ->orwhere("name_abbr_out", 'like', "%$name%")

                ->orwhere("diseases", 'like', "%$name%")

                ->orwhere("code", 'like', "%$name%");

            }

            if (isset($priority)) {

                $query->where("priority", $priority);

            }

sql语句如下:


select * from `trials` where (`priority` = 0 and `name` like '%肺癌%'or `diseases` like '%肺癌%') and `trials`.`deleted_at` is null order by `created_at` desc limit 100000 offset 0

运行结果: 就搜索到了,状态不对的一条(理应是空结果).

laravel常用代码库:where条件查询闭包-具有多个AND OR WHERE条件的复杂MySQL查询

小结

这时候,priority 这个条件 将会与下面大段条件联合在一起. 同时享受了 orwhere.

这就影响了我们对priority条件的先决性.
所以,我们采用括号,括起来.
在.laravel中, 我们就使用闭包就好啦.

本作品采用《CC 协议》,转载必须注明作者和本文链接
嗨,我是波波。曾经创业,有收获也有损失。我积累了丰富教学与编程经验,期待和你互动和进步! 公众号:上海PHP自学中心 付费知识星球:破解面试:程序员的求职导师
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 1
wangchunbo

用户传入查询,我们查询name 和 phone 两个字段. 这么写:+1:


if (!empty($name) || is_numeric($phone)) {
                $query->where(function ($query) use ($name) {

                    $query->where("name", 'like', "%$name%");     

                });
                $query->where(function ($query) use ($phone) {

                    $query->where("phone", 'like', "%$phone%");     

                });
            }
3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
司机 @ 欣昊玉
文章
273
粉丝
339
喜欢
557
收藏
1103
排名:64
访问:12.1 万
私信
所有博文
社区赞助商