快捷查询
快捷查询方式是一种多字段相同查询条件的简化写法,可以进一步简化查询条件的写法,在多个字段之间用|分割表示OR查询,用&分割表示AND查询,可以实现下面的查询,例如:
Db::table('think_user')
->where('name|title','like','thinkphp%')
->where('create_time&update_time','>',0)
->find();
生成的查询SQL是:
SELECT * FROM `think_user` WHERE ( `name` LIKE 'thinkphp%' OR `title` LIKE 'thinkphp%' ) AND ( `create_time` > 0 AND `update_time` > 0 ) LIMIT 1
区间查询
区间查询是一种同一字段多个查询条件的简化写法,例如:
Db::table('think_user')
->where('name',['like','thinkphp%'],['like','%thinkphp'])
->where('id',['>',0],['<>',10],'or')
->find();
生成的SQL语句为:
SELECT * FROM `think_user` WHERE ( `name` LIKE 'thinkphp%' AND `name` LIKE '%thinkphp' ) AND ( `id` > 0 OR `id` <> 10 ) LIMIT 1
批量查询
可以进行多个条件的批量条件查询定义,例如:
Db::table('think_user')
->where([
'name' => ['like','thinkphp%'],
'title' => ['like','%thinkphp'],
'id' => ['>',0],
'status'=> 1
])
->select();
生成的SQL语句为:
SELECT * FROM `think_user` WHERE `name` LIKE 'thinkphp%' AND `title` LIKE '%thinkphp' AND `id` > 0 AND `status` = '1'
闭包查询
Db::table('think_user')
->select(function($query){
$query->where('name','thinkphp')
->whereOr('id','>',10);
});
生成的SQL语句为:
SELECT * FROM `think_user` WHERE `name` = 'thinkphp' OR `id` > 10
混合查询
可以结合前面提到的所有方式进行混合查询,例如:
Db::table('think_user')
->where('name',['like','thinkphp%'],['like','%thinkphp'])
->where(function($query){
$query->where('id',['<',10],['>',100],'or');
})
->select();
生成的SQL语句是:
SELECT * FROM `think_user` WHERE ( `name` LIKE 'thinkphp%' AND `name` LIKE '%thinkphp' ) AND ( `id` < 10 or `id` > 100 )
|