thinkphp where查询条件
文章描述:
thinkphp6 where查询使用方法和where or
表达式
Db::name('auth')->where('name','系统设定')->find();
Db::name('auth')->where('id','>',3)->find();
Db::name('auth')->where('name','系统设定')->where('id','>',3)->find();
数组
关联数组
Db::name('auth')->where(['name' =>'系统设置','id' =>1])->find();
索引数组
Db::name('auth')->where([['name','=','thinkphp'],['id','=',1]])->find();
组装数组查询条件
$map[] = ['name','like','think'];
$map[] = ['is_delete','=',1];
Db::name('auth')->where($map)->find();
字符串条件
Db::name('auth')->whereRaw('name="系统设置" AND id=1')->find();
or
->where('type','=',0)->whereOr('type','=',-1)
if(isset($param['search_value']) && isset($param['search_field'])){
$where[] = array('m.name','like','%'.$param['search_value'].'%');
}
Db::name('user')->whereLike('name','thinkphp%')->select();
Like
->whereLike('customer_code','%' . $param['keyword'] . '%')
where or
// 条件初始化
$where = [
['is_delete', '=', 0],
];
$whereOr = [
['username', '=', '张三'],
['username', '=', '李四'],
];
(new UserModel())->where($where)->where(function ($query) use ($whereOr) { $query->whereOr($whereOr); })->select()->toArray();
执行SQL
SELECT * FROM `tp_users` WHERE `is_delete` = 0 AND ( `username` = ‘张三’ OR `username` = ‘李四’ )
whereIn
->whereIn('user_id',[1,2,3])
执行结果:
SELECT * FROM `tp_user` WHERE `user_id` IN (1,2,3)
whereTime时间查询
->whereTime('job_date','between',[$param['start_date'],$param['end_date']])
执行结果:
SELECT * FROM `tp_user` WHERE register_date BETWEEN ' 2024-11-01' AND '2024-11-30'
发布时间:2023/07/21
发表评论