您的位置:首页 > 编程语言 > PHP开发

ThinkPHP5学习(10)数据库-查询语言

2017-06-07 00:18 585 查看
学习内容:

视频教程:

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
c38f
(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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: