- 开发无止境 -
Data: 2020-01-09 18:16:22Form: JournalClick: 12
连贯操作 | 作用 | 支持的参数类型 |
---|---|---|
where* | 用于 AND 查询 | 字符串、数组和对象 |
table | 用于定义要操作的数据表名称 | 字符串和数组 |
name | 用于定义要操作的数据表名称 | 字符串 |
field* | 用于定义要查询的字段(支持字段排除) | 字符串和数组 |
order* | 用于对结果排序 | 字符串和数组 |
limit | 用于限制查询结果数量 | 字符串和数字 |
page | 用于查询分页(内部会转换成 limit) | 字符串和数字 |
getLastSql | 获取上次执行的 sql 语句 | |
fetchSql | 直接返回当前的 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());
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());
数据库配置
database.php
return [
'connections' => [
'mysql' => [
// 数据库表前缀
'prefix' => Env::get('database.prefix', 'shop_'),
]
]
];
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());
withoutField
withoutField
方法作用 排除数据表中的字段Db::table('shop_goods')->withoutField('id')->select();
fieldRaw
fieldRaw
方法直接使用mysql
函数Db::table('shop_goods')->fieldRaw('id,sum(price)')->select();
order
方法用于对操作的结果排序或者优先级限制asc
正序desc
倒序$select = Db::table('shop_goods')
->field('title,price,id')
->where('status',1)
->order('price','DESC')
->order('id','asc')
->select();
print_r($select->toArray());
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());
方法 | 功能 |
---|---|
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');
print_r($select);
// 获取总数,参数是要统计的字段名(必须)
$select = Db::table('shop_goods')->sum('id');
print_r($select);
controller 代码
public function index(){
$title = '商城';
$login = '欧阳克';
# 左侧菜单
$menu = Db::table('shop_menu')->where('fid',0)->select();
$left = $menu->toArray();
foreach($left as &$left_v){
$left_v['lists'] = Db::table('shop_menu')->where('fid',$left_v['id'])->select();
}
# 右侧列表
$param = Request::param();
if(isset($param['status']) && $param['status'] == 1){
$where[<