thinkphp 5中的混合查询

1.手册样例thinkphp 5.0
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 )
2.手册样例 thinkphp 5.1
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 )
3.function($query)传参数用use()
样例:
Db::table('think_user')
->where($where)
->where(function($query) use($group_id){
$query->where(Db::raw("group_id is null or group_id = '$group_id'")); //Db::raw()可以直接写sql语句查询
})
->select();
Db::table('think_user')
posted @ 2020-05-22 18:08  北往星辰  阅读(640)  评论(0编辑  收藏  举报