这种方式是 TP5 最常用的数组查询格式,适合简单到中等复杂度的查询。
1. 等值查询
适用于 =、<>、>、< 等简单条件,默认是等值匹配。
- // 基础写法:默认 = 匹配
- $where['status'] = 1; // 等同于 status = 1
- $where['id'] = ['>', 10]; // id > 10
- $where['price'] = ['between', [100, 200]]; // price BETWEEN 100 AND 200
- // 多条件组合
- $where = [
- 'status' => 1, // 状态为1
- 'type' => ['in', [1,2,3]], // 类型在1、2、3中
- 'create_time' => ['>', strtotime('2024-01-01')] // 创建时间大于2024-01-01
- ];
- // 执行查询
- $result = Db::name('table')->where($where)->select();
2. 模糊查询
专门用于 like 模糊匹配,是等值查询的扩展写法。
- $q = 'thinkphp';
- $where['title'] = ['like', "%{$q}%"]; // title LIKE '%thinkphp%'
- $where['content'] = ['not like', '%test%']; // content NOT LIKE '%test%'
- // 执行查询
- $result = Db::name('table')->where($where)->select();
二、表达式格式
这种方式更灵活,适合单条件精准控制,格式为 [字段名, 表达式, 查询值]。
- // 你的第三种示例
- $where[] = ['title', 'like', '%thinkphp']; // title LIKE '%thinkphp'
- // 其他常用表达式示例
- $where[] = ['id', '>', 10]; // id > 10
- $where[] = ['status', 'in', [1,2]]; // status IN (1,2)
- $where[] = ['price', 'between', [100, 200]]; // price BETWEEN 100 AND 200
- $where[] = ['name', 'neq', 'admin']; // name != 'admin'
- // 执行查询
- $result = Db::name('table')->where($where)->select();
三、扩展高级查询写法
TP5 还有以下常用的高级查询方式:
1. 闭包查询(复杂条件分组)
适合多条件组合、带括号的复杂查询,优先级最高。
- $result = Db::name('table')
- ->where(function ($query) {
- $query->where('status', 1)->whereOr('type', 2);
- })
- ->where(function ($query) {
- $query->where('create_time', '>', strtotime('2024-01-01'))
- ->where('price', '<', 500);
- })
- ->select();
- // 最终SQL:WHERE (status = 1 OR type = 2) AND (create_time > ... AND price < 500)
2. 字符串条件查询(原生 SQL 片段)
适合极复杂的查询,直接写 SQL 条件片段(注意防 SQL 注入)。
- // 安全写法:带参数绑定
- $result = Db::name('table')
- ->where('status = :status AND id > :id', ['status' => 1, 'id' => 10])
- ->select();
- // 简单写法(不推荐,易注入)
- $result = Db::name('table')->where('status=1 AND id>10')->select();
3. 区间查询(多字段多条件)
一次性给多个字段设置区间条件。
4. 空值查询(IS NULL / IS NOT NULL)
- // 多字段同一条件
- $where = [
- ['id', '>', 10],
- ['id', '<', 100]
- ];
- // 等同于 id > 10 AND id < 100
- // 不同字段不同条件
- $where = [
- 'id' => ['between', [10, 100]],
- 'price' => ['not between', [50, 150]]
- ];
5. 批量 OR 查询
- $where['name'] = ['null']; // name IS NULL
- $where['email'] = ['not null']; // email IS NOT NULL
- $result = Db::name('table')->where($where)->select();
- // 方式1:数组格式
- $where[] = ['status', '=', 1];
- $where[] = ['type', '=', 2, 'or']; // 加or标识
- // 等同于 status = 1 OR type = 2
- // 方式2:whereOr方法
- $result = Db::name('table')
- ->where('status', 1)
- ->whereOr('type', 2)
- ->whereOr('id', '>', 100)
- ->select();
6. 正则查询(仅支持 MySQL)
- $where['name'] = ['regexp', '^think']; // 匹配以think开头的name
- $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']


















文章点评