手册
目录
收藏168
分享
阅读187516
更新时间2022-04-28
前言:
数据库提供的链式操作方法,可以有效的提高数据存取的代码清晰度和开发效率,并且支持所有的CURD操作。
数据库提供的链式操作方法,可以有效的提高数据存取的代码清晰度和开发效率,并且支持所有的CURD操作
带*标识的表示支持多次调用
| 连贯操作 | 作用 | 支持的参数类型 |
| where* | 用于AND查询 | 字符串、数组和对象 |
| table | 用于定义要操作的数据表名称 | 字符串和数组 |
| name | 用于定义要操作的数据表名称 | 字符串 |
| field* | 用于定义要查询的字段(支持字段排除) | 字符串和数组 |
| order* | 用于对结果排序 | 字符串和数组 |
| limit | 用于限制查询结果数量 | 字符串和数字 |
| page | 用于查询分页(内部会转换成limit) | 字符串和数字 |
表达式是SQL语句的条件
表达式不分大小写
表达式写在where里
| 表达式 | 含义 | 查询方法 |
| = | 等于 | |
| <> | 不等于 | |
| > | 大于 | |
| >= | 大于等于 | |
| < | 小于 | |
| <= | 小于等于 | |
| [NOT] LIKE | 模糊查询 | whereLike/whereNotLike |
| [NOT] BETWEEN | (不在)区间查询 | whereBetween/whereNotBetween |
| [NOT] IN | (不在)IN 查询 | whereIn/whereNotIn |
| [NOT] NULL | 查询字段是否(不)是NULL | whereNull/whereNotNull |
where查询
where方法在链式操作方法里面是最常用的方法,可以完成包括普通查询、表达式查询、快捷查询、区间查询、组合查询在内的条件查询操作
# 等于(=)
$select = Db::table('shop_goods')->where('id','=','1')->select();
print_r($select->toArray());
# 不等于(<>)
$select = Db::table('shop_goods')->where('id','<>','2')->select();
print_r($select->toArray());
# 大于(>)
$select = Db::table('shop_goods')->where('id','>','3')->select();
print_r($select->toArray());
# 大于等于(>=)
$select = Db::table('shop_goods')->where('id','>=','4')->select();
print_r($select->toArray());
# 小于(<)
$select = Db::table('shop_goods')->where('id','<','5')->select();
print_r($select->toArray());
# 小于等于(<=)
$select = Db::table('shop_goods')->where('id','<=','6')->select();
print_r($select->toArray());
# 多where
$select = Db::table('shop_goods')
->where('id','>','3')
->where('id','<','8')
->select();
print_r($select->toArray());
# LIKE
$select = Db::table('shop_goods')->where('title','like','%连衣裙%')->select();
print_r($select->toArray());
# NOT LIKE
$select = Db::table('shop_goods')->where('title','not like','%连衣裙%')->select();
print_r($select->toArray());
# BETWEEN
$select = Db::table('shop_goods')->where('id','between','6,10')->select();
print_r($select->toArray());
# NOT BETWEEN
$select = Db::table('shop_goods')->where('id','not between',[6,10])->select();
print_r($select->toArray());
# IN
$select = Db::table('shop_goods')->where('id','in','4,7,10')->select();
print_r($select->toArray());
# NOT IN
$select = Db::table('shop_goods')->where('id','not in',[4,7,10])->select();
print_r($select->toArray());
1、table 和 name
# 必须完整数据库名
$select = Db::table('shop_goods')->where('id','10')->select();
print_r($select->toArray());
# 数据库未设置前缀
$select = Db::name('shop_goods')->where('id','11')->select();
print_r($select->toArray());
# 数据库设置前缀,无前缀访问
$select = Db::name('list')->where('id','12')->select();
print_r($select->toArray());
2、数据库前缀
数据库配置 database.php
return [
'connections' => [
'mysql' => [
// 数据库表前缀
'prefix' => Env::get('database.prefix', 'shop_'),
]
]
];
1、field
field 方法主要作用是标识要返回或者操作的字段,可以用于查询和写入操作
所有的查询方法都可以使用field方法
# 字符串
$select = Db::table('shop_goods')
->field('title,price,discount as d')
->where('status',1)
->select();
print_r($select->toArray());
# 数组
$select = Db::table('shop_goods')
->field([
'title',
'price',
'discount'=>'d'
])
->where('status',1)
->select();
print_r($select->toArray());
# 添加,只能添加这几个字段
# 多field
$data = [
'title' => '新商品',
'price' => 50,
'discount' => 8,
'add_time' => 1576080000
];
$insert = Db::table('shop_goods')
->field('title')
->field('price')
->field('discount')
->field('add_time')
->insert($data);
print_r($insert);
# 查询全部字段,速度较快
$select = Db::table('shop_goods')
->field(true)
// ->field('*')
->where('status',1)
->select();
print_r($select->toArray());
2、withoutField
withoutField 方法作用 排除数据表中的字段
Db::table('shop_goods')->withoutField('id')->select();3、fieldRaw
fieldRaw 方法直接使用mysql函数
Db::table('shop_goods')->fieldRaw('id,sum(price)')->select();1、order 方法用于对操作的结果排序或者优先级限制
默认正序
asc 正序
desc 倒序
$select = Db::table('shop_goods')
->field('title,price,id')
->where('status',1)
->order('price','DESC')
->order('id','DESC')
->select();
print_r($select->toArray());
2、orderRaw 方法中使用mysql函数
$select = Db::table('shop_goods')
->field('title,price,id')
->where('status',1)
->orderRaw("field(title,'price','discount','stock')")
->select();
print_r($select->toArray());
limit 方法主要用于指定查询和操作的数量
$select = Db::table('shop_goods')
->field('title,price,id')
->where('status',1)
->order('price','DESC')
->limit(3)
->select();
print_r($select->toArray());
$select = Db::table('shop_goods')
->field('title,price,id')
->where('status',1)
->order('price','DESC')
->limit(0,5)
->select();
print_r($select->toArray());
page 方法主要用于分页查询
$select = Db::table('shop_goods')
->field('title,price,id')
->where('status',1)
->order('price','DESC')
->page(1,5)
->select();
print_r($select->toArray());
聚合方法如果没有数据,默认都是0,聚合查询都可以配合其它查询条件
| 方法 | 功能 |
| count | 统计数量,参数是要统计的字段名(可选) |
| max | 获取最大值,参数是要统计的字段名(必须) |
| min | 获取最小值,参数是要统计的字段名(必须) |
| avg | 获取平均值,参数是要统计的字段名(必须) |
| sum | 获取总数,参数是要统计的字段名(必须) |
// 统计数量,参数是要统计的字段名(可选)
$select = Db::table('shop_goods')->count();
print_r($select);
// 获取最大值,参数是要统计的字段名(必须)
$select = Db::table('shop_goods')->max('id');
print_r($select);
// 获取最小值,参数是要统计的字段名(必须)
$select = Db::table('shop_goods')->min('id');
print_r($select);
// 获取平均值,参数是要统计的字段名(必须)
$select = Db::table('shop_goods')->avg('id');
p
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.2万人学习
共49课时
77万人学习
共29课时
61.7万人学习
共25课时
39.3万人学习
共43课时
70.9万人学习
共25课时
61.6万人学习
共22课时
23万人学习
共28课时
33.9万人学习
共89课时
125万人学习