侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

thinkphp5中常用数据库查询语句介绍

ThinkPHP  /  管理员 发布于 8年前   448

tp_data 数据表

1.jpg

value()

$name = Db::name('data')-> where('id', 16)-> value('name');print_r($name);// 获取 tp_data 数据表中 id = 16,name 字段的值,并打印// 结果:1111/** 原生sql语句>Prepare SELECT `name` FROM `tp_data` WHERE `id` = ? LIMIT 1>Execute SELECT `name` FROM `tp_data` WHERE `id` = 16 LIMIT 1*/

column()

获取一列满足条件的数据

$list = Db::name('data)-> where('status', 1)-> column('name');print_r($list);// 从 tp_data 数据表获取一列 status = 1 的 name 字段值/** 结果:Array(  [0] => thinkphp  [1] => thinkphp  [2] => thinkphp  [3] => thinkphp  [4] => 7777777777  [5] => thinkphp  [6] => thinkphp  [7] => thinkphp  [8] => thinkphp)*/

获取一列满足条件的数据,并以id值为键名

$list = Db::name('data)-> where('status', 1)-> column('name', 'id');print_r($list);// 从 tp_data 数据表获取一列 status=1 的 name 字段值集合/** 结果:Array(  [3]  => thinkphp  [4]  => thinkphp  [5]  => thinkphp  [6]  => thinkphp  [7]  => 7777777777  [8]  => thinkphp  [9]  => thinkphp  [10] => thinkphp  [11] => thinkphp)*/

获取以id为键名的数据集

$list = Db::name('data')    -> where('status', 1)    -> column('*', 'id');print_r($list);// 从 tp_data 数据表获取一列 status=1 的数据集/** 结果:Array(  [3] => Array(        [id] => 3        [name] => thinkphp        [status] => 1      )  [4] => Array(        [id] => 4        [name] => thinkphp        [status] => 1      )  [5] => Array(        [id] => 5        [name] => thinkphp        [status] => 1      )  ...)*/

聚合查询

count

max

min

avg

sum

统计 data 表的数据

$count = Db::name('data')-> where('status', 1)-> count();echo $count;// 结果:9

统计 data 表的最大 id

$max = Db::name('data')-> where('status', 1)-> max('id);echo $max;// 结果:11

简单查询

$result = Db::name('data')-> where("id > :id and name like :name",['id' => 10,'name' => "%php%"])-> select();print_r($result);/** 结果:Array(  [0] => Array(          [id] => 11          [name] => thinkphp          [status] => 1      ))*//** 原生sql语句:>Prepare SELECT * FROM `tp_data` WHERE (id > ? and name like ?)>Execute SELECT * FROM `tp_data` WHERE (id > '10' and name like '%php%')*/

日期查询

日期类型int,时间戳格式

查询时间大于 2016-1-1 的数据

$result = Db::name('users')-> whereTime('reg_time', '>', '2016-1-1')-> select();print_r($result);/** 原生sql语句:>Prepare SELECT * FROM `tp_users` WHERE `reg_time` > ?>Execute SELECT * FROM `tp_users` WHERE `reg_time` > 1451577600*/

查询本周

$result = Db::name('users')-> whereTime('reg_time', '>', 'this week')-> select();print_r($result);// 从本周星期一开始

查询最近两天添加的数据

$result = Db::name('users')-> whereTime('reg_time', '>', '-2 days')-> select();print_r($result);

查询创建时间在 2016-1-1 ~ 2017-7-1 的数据

$result = Db::name('users')-> whereTime('reg_time', 'between', ['2016-1-1', '2017-7-1'])-> select();print_r($result);/** 原生sql语句:>Prepare SELECT * FROM `tp_users` WHERE `reg_time` BETWEEN ? AND ?>Execute SELECT * FROM `tp_users` WHERE `reg_time` BETWEEN 1451577600 AND 1483200000*/

查询今天的数据

昨天:yesterday

本周:week

上周:last week

$result = Db::name('users')-> whereTime('reg_time', 'today')-> select();print_r($result);

分块查询

Db::name('data')-> where('status', '>', 0)-> chunk(2, function($list) {    foreach($list as $data) {        //处理2条记录    }});/** 原生sql语句:>Prepare SELECT * FROM `tp_data` WHERE `status` > ? ORDER BY `id` asc LIMIT 2>Execute SELECT * FROM `tp_data` WHERE `status` > 0 ORDER BY `id` asc LIMIT 2>Close stmt>Prepare SELECT * FROM `tp_data` WHERE `status` > ? AND `id` > ? ORDER BY `id` asc LIMIT 2>Execute SELECT * FROM `tp_data` WHERE `status` > 0 AND `id` > 4 ORDER BY `id` asc LIMIT 2>Close stmt>Prepare SELECT * FROM `tp_data` WHERE `status` > ? AND `id` > ? ORDER BY `id` asc LIMIT 2>Execute SELECT * FROM `tp_data` WHERE `status` > 0 AND `id` > 6 ORDER BY `id` asc LIMIT 2>Close stmt...>Prepare SELECT * FROM `tp_data` WHERE `status` > ? AND `id` > ? ORDER BY `id` asc LIMIT 2>Execute SELECT * FROM `tp_data` WHERE `status` > 0 AND `id` > 16 ORDER BY `id` asc LIMIT 2>Close stmt>Prepare SELECT * FROM `tp_data` WHERE `status` > ? AND `id` > ? ORDER BY `id` asc LIMIT 2>Execute SELECT * FROM `tp_data` WHERE `status` > 0 AND `id` > 17 ORDER BY `id` asc LIMIT 2>Close stmt*/

改进

$p = 0;do {  $result = Db::name('data') -> limit($p, 2) -> select();  $p += 2;  //处理数据} while(count($result) > 0);

推荐教程:《TP5》

以上就是thinkphp5中常用数据库查询语句介绍的详细内容,更多请关注其它相关文章!


  • 上一条:
    thinkphp5多数据库配置介绍
    下一条:
    关于TP5框架中Query.php中废弃写法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • thinkphp + mongodb项目中数据加载慢问题分析及解决(0个评论)
    • thinkphp6框架中封装redis操作类(0个评论)
    • thinkphp6框架中实现定时任务功能流程步骤(0个评论)
    • Thinkphp5.1框架中实现Session+Redis会话共享流程步骤(0个评论)
    • TP5框架版本5.0.10安全漏洞根据官方补丁修复,也是本站安全漏洞修复(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-12
    • 2018-01
    • 2018-02
    • 2020-03
    • 2021-07
    • 2021-12
    • 2022-05
    • 2022-06
    • 2022-09
    • 2023-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客