• Thinkphp5的where查询


    目录

    一、get 获取一条记录

    二、all 获取多条记录

    三、find 查询某一条

    四、select 多条查询

    五、value 按字段查询一条

    六、查询数目

    七、whereTime() 时间条件查询

    八、where查询条件

    九、where表达式查询


    一、get 获取一条记录

    $res = User::get(1);

    二、all 获取多条记录

    1、不传参

    $result = User::all(); //查询出所有记录 

    2、参数为n,n为正整数 

    $result = User::all(1); //查询出id为1的记录

    3、参数为'n1, n2, n3...'

    $result = User::all('7, 8, 9, 10'); //查询出id为7、8、9、10的4条记录 

    4、参数为[n1, n2, n3...] 

    $result = User::all([7, 8, 9, 10]); //查询出id为7、8、9、10的4条记录

    三、find 查询某一条

     $res = User::where('id','1')->field('name')->find();

    四、select 多条查询

    $res = User::where('id','1')->field('name')->limit(2)->order('id DESC')->select();

    五、value 按字段查询一条

    $res = User::where('id','1')->value('name');

    六、查询数目

    1. //查询总条数
    2. $res = User::count();
    3. //按条件统计条数
    4. $res = User::where('id','>',3)->count();

    七、whereTime() 时间条件查询

    1、获取今天的数据

    1. db('table')->whereTime('c_time', 'today')->select();
    2. //也可以简化为下面方式
    3. db('table')->whereTime('c_time', 'd')->select();

    2、获取昨天的数据

    db('table')->whereTime('c_time', 'yesterday')->select();

    3、获取本周的数据

    1. db('table')->whereTime('c_time', 'week')->select();
    2. //也可以简化为下面方式
    3. db('table')->whereTime('c_time', 'w')->select(); 

    4、获取本月的数据

    1. db('table')->whereTime('c_time', 'month')->select();   
    2. //也可以简化为下面方式
    3. db('table')->whereTime('c_time', 'm')->select();   

     5、获取上月的数据

    db('table')->whereTime('c_time','last month')->select();    

    6、获取今年的数据

    1. db('table')->whereTime('c_time', 'year')->select();    
    2. //也可以简化为下面方式
    3. db('table')->whereTime('c_time', 'y')->select();    

    7、获取去年的数据

    db('table')->whereTime('c_time','last year')->select(); 

    8、日期区间查询

    1. //根据时间戳查询今天到后天
    2. db('table')->whereTime('time', 'between', [strtotime(date('Y-m-d')), strtotime(date('Y-m-d', strtotime('+2 day')))])->select();
    3. //根据日期查询今天到后天
    4. db('table')->whereTime('time', 'between', ['2020-3-28', '2020-3-30'])->select();

    八、where查询条件

    方式1:

    一个参数,字符串条件直接查询,例如:where("id>10")
    方式2:

    一个参数,字段名为键的关联数组,例如:where(['id'=>array('eq',10)])
    方式3:

    二个参数,第一个为字段名,第二个参数为字段值,例如:where('id',10)
    方式4:

    三个参数,第一个为字段名,第二个为条件,第三个参数为值,例如:where('id','eq',10)

     1、使用字符串条件+预处理机制一起使用

    1. //方式1
    2. $res = db('table')->where("id=:id and type=:type")->bind(['id'=>15,'type'=>0])->select();
    3. //方式2
    4. $res = db('table')->where("id=? and type=?")->bind([15,0])->select();
    5. //方式3
    6. $res = db('table')->where("id=:id and type=:type",array('id'=>$id,'type'=>$type))->select();
    7. //方式4
    8. $res = db('table')->where("id=? and type=?",array($id,$type))->select();

    2、使用数组作为条件,进行普通查询

    1. $map = array(
    2. 'id'=>15,
    3. 'type'=>0,
    4. );
    5. $res = db('table')->where($map)->select();

    九、where表达式查询

    语法:

    $map[‘字段1’] = array(‘表达式’,‘查询条件1’);

    $map[‘字段2’] = array(‘表达式’,‘查询条件2’);

    db(‘table’)->where($map)->select();

    1、表达式不分大小写,支持的查询表达式有下面几种,分别表示的含义是:

    表达式含义
    eq、=等于(=)
    neq、<>不等于(<>)
    gt、>大于(>)
    egt、>=大于等于(>=)
    lt、<小于(<)
    elt、<=小于等于(<=)
    like模糊查询
    [not] between(不在)区间查询
    [not] in(不在)IN 查询
    [not] null查询字段是否(不)是NULL
    [not] existsEXISTS查询
    exp表达式查询,支持SQL语法
    > time时间比较
    < time时间比较
    between time时间比较
    notbetween time时间比较

     2、表达式查询的用法示例如下:

    eq :等于(=)

    例如:

    1. where('id','eq',100);
    2. where('id','=',100);

    和下面的查询等效

    where('id',100);
    

    表示的查询条件就是 id = 100

    neq: 不等于(<>)

    例如:

    1. where('id','neq',100);
    2. where('id','<>',100);

    表示的查询条件就是 id <> 100

    gt:大于(>)

    例如:

    1. where('id','gt',100);
    2. where('id','>',100);

    表示的查询条件就是 id > 100

    egt:大于等于(>=)

    例如:

    1. where('id','egt',100);
    2. where('id','>=',100);

    表示的查询条件就是 id >= 100

    lt:小于(<)

    例如:

    1. where('id','lt',100);
    2. where('id','<',100);

    表示的查询条件就是 id < 100

    elt: 小于等于(<=)

    例如:

    1. where('id','elt',100);
    2. where('id','<=',100);

    表示的查询条件就是 id <= 100

    [not] like:用法等同于sql的like

    例如:

    where('name','like','thinkphp%');
    

    查询条件就变成 name like 'thinkphp%'

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

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

    [not] between :用法等同于sql的[not] between

    查询条件支持字符串或者数组,例如:

    where('id','between','1,8');
    

    和下面的等效:

    where('id','between',[1,8]);
    

    查询条件就变成 id BETWEEN 1 AND 8

    [not] in: 用法等同于sql的[not] in

    查询条件支持字符串或者数组,例如:

    where('id','not in','1,5,8');
    

    和下面的等效:

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

    查询条件就变成 id NOT IN (1,5, 8)

    [NOT] IN查询支持使用闭包方式

    [not] null

    查询字段是否(不)是Null,例如:

    1. where('name', null);
    2. where('title','null');
    3. where('name','not null');

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

    1. where('title','=', 'null');
    2. where('name','=', 'not null');

    exp:表达式

    支持更复杂的查询情况 例如:

    where('id','in','1,3,8');
    

    可以改成:

    where('id','exp',' IN (1,3,8) ');
    

    exp查询的条件不会被当成字符串,所以后面的查询条件可以使用任何SQL支持的语法,包括使用函数和字段名称。

  • 相关阅读:
    通过此文让你全面了解Thread线程的基本操作
    操作系统原理,IO控制方式,轮询流程,中断驱动流程,设备IO部件演化;IO的软件组成与层次,设备独立性;IO相关技术,缓冲技术
    通讯网关软件025——利用CommGate X2Modbus实现Modbus RTU访问DDE数据源
    FastJson 漏洞复现
    RabbitMQ的高级特性
    Netty 学习(六):创建 NioEventLoopGroup 的核心源码说明
    Java基础之正则表达式
    Linux权限维持
    限流与下载接口请求数控制
    驾辰龙跨Llama持Wasm,玩转Yi模型迎新春
  • 原文地址:https://blog.csdn.net/qq15577969/article/details/128052628