QQ咨询 官方微信

添加微信好友

官方小程序

百度小程序

微信小程序

头条小程序

关于我们

Thinkphp

thinkphp5常用查询where写法

 shitouadmin  2026-01-21 22:00:47
一、基础数组格式
这种方式是 TP5 最常用的数组查询格式,适合简单到中等复杂度的查询。
1. 等值查询
适用于 =、<>、>、< 等简单条件,默认是等值匹配。
  1. // 基础写法:默认 = 匹配 
  2. $where['status'] = 1; // 等同于 status = 1 
  3. $where['id'] = ['>', 10]; // id > 10 
  4. $where['price'] = ['between', [100, 200]]; // price BETWEEN 100 AND 200 
  1. // 多条件组合 
  2. $where = [ 
  3.     'status' => 1, // 状态为1 
  4.     'type' => ['in', [1,2,3]], // 类型在1、2、3中 
  5.     'create_time' => ['>', strtotime('2024-01-01')] // 创建时间大于2024-01-01 
  6. ]; 
  1. // 执行查询 
  2. $result = Db::name('table')->where($where)->select(); 

2. 模糊查询
专门用于 like 模糊匹配,是等值查询的扩展写法。
  1. $q = 'thinkphp'
  2. $where['title'] = ['like'"%{$q}%"]; // title LIKE '%thinkphp%' 
  3. $where['content'] = ['not like''%test%']; // content NOT LIKE '%test%' 
  4.  
  5. // 执行查询 
  6. $result = Db::name('table')->where($where)->select(); 

二、表达式格式
这种方式更灵活,适合单条件精准控制,格式为 [字段名, 表达式, 查询值]。
  1. // 你的第三种示例 
  2. $where[] = ['title''like''%thinkphp']; // title LIKE '%thinkphp' 
  3.  
  4. // 其他常用表达式示例 
  5. $where[] = ['id''>', 10]; // id > 10 
  6. $where[] = ['status''in', [1,2]]; // status IN (1,2) 
  7. $where[] = ['price''between', [100, 200]]; // price BETWEEN 100 AND 200 
  8. $where[] = ['name''neq''admin']; // name != 'admin' 
  9.  
  10. // 执行查询 
  11. $result = Db::name('table')->where($where)->select(); 

三、扩展高级查询写法
TP5 还有以下常用的高级查询方式:
1. 闭包查询(复杂条件分组)
适合多条件组合、带括号的复杂查询,优先级最高。
  1. $result = Db::name('table'
  2.     ->where(function ($query) { 
  3.         $query->where('status', 1)->whereOr('type', 2); 
  4.     }) 
  5.     ->where(function ($query) { 
  6.         $query->where('create_time''>', strtotime('2024-01-01')) 
  7.               ->where('price''<', 500); 
  8.     }) 
  9.     ->select(); 
  10. // 最终SQL:WHERE (status = 1 OR type = 2) AND (create_time > ... AND price < 500) 

2. 字符串条件查询(原生 SQL 片段)
适合极复杂的查询,直接写 SQL 条件片段(注意防 SQL 注入)。
  1. // 安全写法:带参数绑定 
  2. $result = Db::name('table'
  3.     ->where('status = :status AND id > :id', ['status' => 1, 'id' => 10]) 
  4.     ->select(); 
  5.  
  6. // 简单写法(不推荐,易注入) 
  7. $result = Db::name('table')->where('status=1 AND id>10')->select(); 

3. 区间查询(多字段多条件)
一次性给多个字段设置区间条件。
  1. // 多字段同一条件 
  2. $where = [ 
  3.     ['id''>', 10], 
  4.     ['id''<', 100] 
  5. ]; 
  6. // 等同于 id > 10 AND id < 100 
  7.  
  8. // 不同字段不同条件 
  9. $where = [ 
  10.     'id' => ['between', [10, 100]], 
  11.     'price' => ['not between', [50, 150]] 
  12. ]; 
4. 空值查询(IS NULL / IS NOT NULL)
  1. $where['name'] = ['null']; // name IS NULL 
  2. $where['email'] = ['not null']; // email IS NOT NULL 
  3.  
  4. $result = Db::name('table')->where($where)->select(); 
5. 批量 OR 查询
  1. // 方式1:数组格式 
  2. $where[] = ['status''=', 1]; 
  3. $where[] = ['type''=', 2, 'or']; // 加or标识 
  4. // 等同于 status = 1 OR type = 2 
  5.  
  6. // 方式2:whereOr方法 
  7. $result = Db::name('table'
  8.     ->where('status', 1) 
  9.     ->whereOr('type', 2) 
  10.     ->whereOr('id''>', 100) 
  11.     ->select(); 

6. 正则查询(仅支持 MySQL)
  1. $where['name'] = ['regexp''^think']; // 匹配以think开头的name 
  2. $result = Db::name('table')->where($where)->select(); 

四、TP5 支持的所有查询表达式
表达式 含义 示例
= 等于 ['id', '=', 10]
<> 不等于 ['id', '<>', 10]
> 大于 ['id', '>', 10]
< 小于 ['id', '<', 10]
>= 大于等于 ['id', '>=', 10]
<= 小于等于 ['id', '<=', 10]
like 模糊匹配 ['title', 'like', '%think%']
not like 不模糊匹配 ['title', 'not like', '%test%']
in 包含 ['id', 'in', [1,2,3]]
not in 不包含 ['id', 'not in', [1,2,3]]
between 区间 ['price', 'between', [100,200]]
not between 不在区间 ['price', 'not between', [100,200]]
null 为空 ['name', 'null']
not null 不为空 ['name', 'not null']
regexp 正则匹配 ['name', 'regexp', '^think']


¥ 打赏
×
如果您觉得文章帮助了您就打赏一下吧
非常感谢你的打赏,我们将继续分享更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

本文《thinkphp5常用查询where写法》发布于石头博客文章,作者:shitouadmin,如若转载,请注明出处:https://www.pweb123.com/kuangjia/thinkphp/1231.html,否则禁止转载,谢谢配合!

文章点评

我来说两句 已有0条评论
点击图片更换

添加微信好友

添加微信好友

微信小程序

百度小程序