ThinkPHP5学习(10)数据库-查询语言
来源:互联网 发布:淘宝买家订单能导出吗 编辑:程序博客网 时间:2024/06/10 19:58
学习内容:
视频教程:
https://www.kancloud.cn/tpshop/thinkphp5/225450
完全开发手册:
https://www.kancloud.cn/manual/thinkphp5/135181
初学如果去看完全开发手册,会要花很多时间。
看视频可以快速的掌握,重要的、实用的内容。
-查询表达式
-批量查询
-快捷查询
-视图查询
-使用Query对象
-获取数据
-获取列数据
-聚合查询
-字符串查询
-日期查询
-分块查询
再建一个表
CREATE TABLE tp_data
(
id
int(11) NOT NULL,
name
varchar(45) DEFAULT NULL,
status
tinyint(1) DEFAULT NULL,
score
tinyint(2) DEFAULT NULL,
email
varchar(45) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SELECT * FROM tp5.tp_data;
做视图,我们在user表中增加两个字段:
ALTER TABLE tp5
.tp_user
ADD COLUMN nickname
VARCHAR(45) NULL AFTER email
,
ADD COLUMN mobile
VARCHAR(20) NULL AFTER nickname
;
做日期查询,添加create_time字段:
ALTER TABLE tp5
.tp_data
ADD COLUMN create_time
DATETIME NULL AFTER score
;
public function hello6(){ $result = Db::name('data')->where('id',4)->find(); // 可以写成 >= <= <> in[4,5,6,7,8] 'between',[5,8] $result = Db::name('data')->where('id','between',[1,9])->select(); print_r($result); // 查询某个字段是否为NULL $result = Db::name('data') ->where('name',null) ->select(); print_r($result); // 使用EXP条件表达式,表示后面是原生的SQL语句表达式 // 按原生sql语句写 // $result = Db::name('data')->where('id','exp'," in(1,2,3,4)")->select(); $result = Db::name('data')->where('id','exp'," >1 and name='111'")->select(); print_r($result); // 使用多个字段查询 $result = Db::name('data') ->where('id','>=',1) ->where('name','like','%php%') ->select(); print_r($result); // 或者使用 $result = Db::name('data') ->where([ 'id'=>['>=',1], 'name'=>['like','%think%'] ])->select(); print_r($result); // 使用OR,AND混合条件查询 $result = Db::name('data') ->where('name','like','%think%') ->where('id',['in',[1,2,3]],['>=',1],'or') ->limit(2) ->select(); print_r($result); // 批量查询 $result = Db::name('data') ->where([ 'id'=>[['in',[1,2,3]],['>=',1],'or'], 'name'=>['like','%php%'] ]) ->limit(10) ->select(); print_r($result); // 快捷查询 $result = Db::name('data') ->where('id&status','>',0) ->limit(10) ->select(); print_r($result); $result = Db::name('data') ->where('id|status','>',0) ->limit(10) ->select(); print_r($result); } public function hello7(){ // 视图查询 // $result = Db::view('data','id,name,status') ->view('user',['nickname'=>'username','mobile','email'],'user.id=data.id') ->where('data.status',1) ->select(); print_r($result); // 使用Query对象 $query = new \think\db\Query; $query->name('data')->where('name','like','%think%') ->where('id','>=',1) ->limit(10); $result = Db::select($query); print_r($result); // 时间(日期)查询 // 查询创建时间>'2017-01-01'的数据 $result = Db::name('data') ->whereTime('create_time','>','2017-01-01') ->select(); dump($result); // 查询本周添加的数据 $result = Db::name('data') ->whereTime('create_time','>','this week') ->select(); dump($result); // 查询近两天添加的数据 $result = Db::name('data') ->whereTime('create_time','>','-2 days') ->select(); dump($result); // 查询创建日期在2017-01-01~2018-01-01的数据 $result= Db::name('data') ->whereTime('create_time','between',['2017-01-01','2018-01-01']) ->select(); dump($result); // 字符串查询 $result = Db::name('data') ->where('id>:id AND name IS NOT NULL',['id'=>10]) ->select(); dump($result); // 获取某行某列某个值 $name = Db::name('data')->where('id',16)->value('name'); print($name); // 获取某列 $name = Db::name('data') ->where('status',1) ->column('name'); print_r($result); // 获取id键名name为值的,键值对,这个有用!比如下拉框取某人 $list = Db::name('data') ->where('status',1) ->column('name','id'); print_r($list); // 获取id键名的数据集 $list = Db::name('data') ->where('status',1) ->column('*','id'); print_r($list); // 聚合查询 count max min avg sum // 统计data表的数据 $max = Db::name('data')->where('status',1)->max('id'); echo $max; // 建议字符串,简单查询,为了防注入,用占位符方式 $result = Db::name('data') ->where("id>:id and name like :name",['id'=>10,'name'=>'%php%']) ->select(); print_r($result); // 日期查询 建议日期类型为int // 查询时间>'2017-01-01'的数据 $result = Db::name('user') ->whereTime('reg_time','>','2017-01-01') ->select(); dump($result); // 查询日期在2017-01-01~2018-01-01的数据 $result= Db::name('user') ->whereTime('reg_time','between',['2017-01-01','2018-01-01']) ->select(); dump($result); // 查询今天 昨天 本周 上周 $result= Db::name('user') ->whereTime('reg_time','today') ->select(); dump($result); // 分块查询 Db::name('data') ->where('status','>',0) ->chunk(2,function($list){ foreach($list as $data){ // 处理2条记录 } }); // 改造后 $p = 0; do{ $result = Db::name('data')->limit($p,2)->select(); $p +=2; print $result; }while(count($result)>0); }
- ThinkPHP5学习(10)数据库-查询语言
- Thinkphp5学习(16)查询范围
- ThinkPHP5学习(8)数据库-基本操作
- ThinkPHP5学习(9)数据库-事务,链式操作
- thinkphp5.0学习(七):数据库操作
- thinkphp5数据库
- thinkphp5,tpshop5下的数据库查询构造器的思考
- Thinkphp5 联合(关联)查询
- ThinkPHP5-缓存查询
- thinkPHP5 分页查询
- 数据库查询语言QL
- 易语言查询数据库
- Sql数据库查询语言
- 数据库查询语言/SELECT
- 数据库查询语言
- DQL数据库查询语言
- 数据库查询语言分类
- 0014 SQL 学习之感悟 数据库查询语言
- 第一个只出现一次的字符
- Android RelativeLayout(相对布局的一些属性)
- 自己写一个linux的系统调用
- 197.m1-listView显示app的所有条目信息
- Java的静态方法不能被重写(详解!)
- ThinkPHP5学习(10)数据库-查询语言
- ITS_MY_CSDN__MY_F&L
- POJ2914_Minimum Cut_无向图最小割::stoer_wagner算法
- Go Log模块
- Python正则表达式指南
- Leetcode:455. Assign Cookies
- ac自动机 hdu 2896. 病毒侵袭
- Android开发之数据库的拷贝
- 设计模式的学习(一)