• ThinkPHP5 WHERE AND OR 实现多条件查询


    问题:ThinkPHP查询条件中包含 AND 和 OR 的条件该如何编写代码。

    1、查询条件

     1 // 基本查询条件
     2 $where = [
     3     ['is_shelves',                '=', 1],
     4     ['is_delete_time',            '=', 0],
     5 ];
     6 
     7 // 关键词查询
     8 $where_keywords = [
     9     ['title|address|vice_title|describe', 'LIKE', '%' . $params['keyword'] . '%'],
    10 ];
    11 
    12 // 范围查询条件
    13 $where_range = [
    14     ['lng', '>=', $range['minlng']],
    15     ['lng', '<=', $range['maxlng']],
    16     ['lat', '>=', $range['minlat']],
    17     ['lat', '<=', $range['maxlat']],
    18 ];

    2、使用ThinkPHP5进行查询

     1 $data = Db::name('ParkApply')->field($field)
     2     ->where(function($query) use ($where){
     3         $query->where($where);
     4     })
     5     ->where(function($query) use ($where_range, $where_keywords){
     6         $query->where($where_keywords);
     7         $query->whereOr(function ($query) use($where_range) {
     8             $query->where($where_range);
     9         });
    10     })
    11     ->order($order_by)->limit($m, $n)->select();

    3、生成的SQL

    1 SELECT * FROM `s_park_apply` WHERE  (  `is_shelves` = 1  AND `is_delete_time` = 0 )  AND (  ( `title` LIKE '%银辉路%' OR `address` LIKE '%银辉路%' OR `vice_title` LIKE '%银辉路%' OR `describe` LIKE '%银辉路%' ) OR (  `lng` >= 105.4528834087  AND `lng` <= 105.4631557269  AND `lat` >= 28.89358913307  AND `lat` <= 28.90258234913 ) ) ORDER BY `id` DESC LIMIT 0,20

    参考博客链接:

    ThinkPHP 5.0 组合多条件whereOr查询

    https://blog.csdn.net/weixin_43837229/article/details/101535227

    ThinkPHP5框架where实现or查询的两种方法

    https://blog.csdn.net/weixin_42330073/article/details/86496940

  • 相关阅读:
    提高电脑运行效率
    Android_实验小心得_持续补充中......
    LNMP环境搭建wordpress
    php安装
    mysql、MariaDB(yum)
    Nginx配置(yum)
    httpd配置(yum)
    jumpserver环境搭建
    命令
    vsftpd
  • 原文地址:https://www.cnblogs.com/hakulamatata/p/14050809.html
Copyright © 2020-2023  润新知