×

thinkphp 数据库 方法

thinkphp5 Db各种查询数据库的方法整合

老余 老余 发表于2023-06-15 08:47:54 浏览238 评论0

抢沙发发表评论


默认条件查询

Db::table('think_user')->where('id',1)->find();


指定字段查询

thinkphp5 Db各种查询数据库的方法整合

Db::table('think_user')->field('id,title,content')->select();
Db::table('think_user')->field('id,SUM(score)')->select();
Db::table('think_user')->field(['id','title','content'])->select();


字段排除

/*
如果我希望获取排除数据表中的content字段(文本字段的值非常耗内存)之外的所有字段值,
我们就可以使用field方法的排除功能,例如下面的方式就可以实现所说的功能:
*/
Db::table('think_user')->field('content',true)->select();
//则表示获取除了content之外的所有字段,要排除更多的字段也可以:
Db::table('think_user')->field('user_id,content',true)->select();
//或者用
Db::table('think_user')->field(['user_id','content'],true)->select();


order排序查询

Db::table('think_user')->where('status=1')->order('id desc,status desc')->limit(5)->select();


V5.0.17+版本开始,当你的order排序中使用了SQL函数的时候,请使用orderRaw方法替代order,例如

Db::table('think_user')->where('status=1')->orderRaw('rand()')->limit(5)->select();


imit方法限制结果数量

Db::table('think_user')->where('status=1')->limit(10)->select();
//从第10行开始的25条数据
Db::table('think_article')->limit('10,25')->select();
Db::table('think_article')->limit(10,25)->select();


指定条件

Db::table('think_user')
    ->where('name','like','%thinkphp')
    ->where('status',1)
    ->find();


多字段相同条件的AND查询可以简化为如下方式

Db::table('think_user')
    ->where('name&title','like','%thinkphp')
    ->find();


whereOr查询

Db::table('think_user')
    ->where('name','like','%thinkphp')
    ->whereOr('title','like','%thinkphp')
    ->find();


使用whereOr方法进行OR查询
多字段相同条件的OR查询可以简化为如下方式:

Db::table('think_user')
    ->where('name|title','like','%thinkphp')
    ->find();


在多个字段之间用|分割表示OR查询,用&分割表示AND查询

Db::table('think_user')
    ->where('name|title','like','thinkphp%')
    ->where('create_time&update_time','>',0)
    ->find();
Db::table('think_user')
    ->where('name',['like','thinkphp%'],['like','%thinkphp'])
    ->where('id',['>',0],['<>',10],'or')
    ->find();


where('id','not in','1,5,8'); 
等效
where('id','not in',[1,5,8]);


如果你需要查询一个字段的值为字符串null或者not null,应该使用:

where('title','=', 'null');
where('name','=', 'not null');


V5.0.4+开始,ThinkPHP支持对同一个字段多次调用查询条件,例如:

Db::table('think_user')
    ->where('name','like','%think%')
    ->where('name','like','%php%')
    ->where('id','in',[1,5,80,50])
    ->where('id','>',10)
    ->find();


V5.0.5+版本开始,like查询支持使用数组

where('name','like',['%think','php%'],'OR');


where('id','between','1,8');
和下面的等效:
where('id','between',[1,8]);


表达式查询


$map['id']  = ['>',1];
$map['mail']  = ['like','%thinkphp@qq.com%'];
Db::table('think_user')->where($map)->select(); 
Db::table('think_user')
    ->where([
        'name'  =>  ['like','thinkphp%'],
        'title' =>  ['like','%thinkphp'],
        'id'    =>  ['>',0],
        'status'=>  1
    ])
    ->select();


字符串条件

Db::table('think_user')->where('type=1 AND status=1')->select(); 
Db::table('think_user')
    ->where('id > 0 AND name LIKE "thinkphp%"')
    ->select();


//或者根据字段统计:
Db::table('think_user')->count('id');
//获取用户的最大积分:
Db::table('think_user')->max('score');
//获取积分大于0的用户的最小积分:
Db::table('think_user')->where('score>0')->min('score');
//获取用户的平均积分:
Db::table('think_user')->avg('score');
//统计用户的总成绩:
Db::table('think_user')->sum('score');


// 大于某个时间
Db::table('think_user')->whereTime('birthday', '>=', '1970-10-1')->select();
// 小于某个时间
Db::table('think_user')->whereTime('birthday', '<', '2000-10-1')->select();
// 时间区间查询
Db::table('think_user')->whereTime('birthday', 'between', ['1970-10-1', '2000-10-1'])->select();
// 不在某个时间区间
Db::table('think_user')->whereTime('birthday', 'not between', ['1970-10-1', '2000-10-1'])->select();


// 获取今天的博客
Db::table('think_blog') ->whereTime('create_time', 'today')->select();
// 获取昨天的博客
Db::table('think_blog')->whereTime('create_time', 'yesterday')->select();
// 获取本周的博客
Db::table('think_blog')->whereTime('create_time', 'week')->select();   
// 获取上周的博客
Db::table('think_blog')->whereTime('create_time', 'last week')->select();    
// 获取本月的博客
Db::table('think_blog')->whereTime('create_time', 'month')->select();   
// 获取上月的博客
Db::table('think_blog')->whereTime('create_time', 'last month')->select();      
// 获取今年的博客
Db::table('think_blog')->whereTime('create_time', 'year')->select();    
// 获取去年的博客
Db::table('think_blog')->whereTime('create_time', 'last year')->select();


如果查询当天、本周、本月和今年的时间,还可以简化为:

// 获取今天的博客
Db::table('think_blog')->whereTime('create_time', 'd')->select();
// 获取本周的博客
Db::table('think_blog')->whereTime('create_time', 'w')->select();   
// 获取本月的博客
Db::table('think_blog')->whereTime('create_time', 'm')->select();   
// 获取今年的博客
Db::table('think_blog')->whereTime('create_time', 'y') ->select();


V5.0.5+版本开始,还可以使用下面的方式进行时间查询
查询两个小时内的博客

Db::table('think_blog')->whereTime('create_time','-2 hours')->select();


如果你需要处理成千上百条数据库记录,可以考虑使用chunk方法,该方法一次获取结果集的一小块,然后填充每小块数据到要处理的闭包,该方法在编写处理大量数据库记录的时候非常有用。

//比如,我们可以全部用户表数据进行分批处理,每次处理 100 个用户记录:
Db::table('think_user')->where('score','>',80)->chunk(100, function($users) {
    foreach ($users as $user) {
        //
    }
});
// 或者交给回调方法myUserIterator处理
Db::table('think_user')->chunk(100, 'myUserIterator');
//chunk方法的处理默认是根据主键查询,支持指定字段,例如:
Db::table('think_user')->chunk(100, function($users) {
    // 处理结果集...
    return false;
},'create_time');
//V5.0.11版本开始,chunk方法支持指定处理数据的顺序。
Db::table('think_user')->chunk(100, function($users) {
    // 处理结果集...
    return false;
},'create_time', 'desc');


不进行查询只是返回构建SQL

//当select方法的参数为false的时候,表示不进行查询只是返回构建SQL,例如:
$subQuery = Db::table('think_user')
    ->field('id,name')
    ->where('id','>',10)
    ->select(false); 
    
//fetchSql方法表示不进行查询而只是返回构建的SQL语句,并且不仅仅支持select,而是支持所有的CURD查询。
$subQuery = Db::table('think_user')
    ->field('id,name')
    ->where('id','>',10)
    ->fetchSql(true)
    ->select();


v5.1版本

$where[] = ['title','like',"%".$sotitle."%"];
$where[] = ['name','like','think'];
$where[] = ['status','=',1];
Db::table('think_user')->where($where)->select();


关联查询
关联查询

$rows = Db::table('think_user')
   ->alias('s')//别名
   ->join('goods g','s.id=g.store_id','left')//查询关联的信息与左表(及s表)不相关的信息
   ->join('user u','s.user_id=u.id')
    ->where('s.id',1)//条件
   ->field('s.*, g.*, u.username')//要查的字段
   ->select();


完毕
————————————————
版权声明:本文为CSDN博主「向宇it」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36303853/article/details/120081175