• Welcome to Journal web site.

我是 PHP 程序员

- 开发无止境 -

Next
Prev

ThinkPHP6 数据库链式操作

Data: 2020-01-09 18:16:22Form: JournalClick: 12

# ThinkPHP6 数据库链式操作

  • 数据库提供的链式操作方法,可以有效的提高数据存取的代码清晰度和开发效率,并且支持所有的 CURD 操作
  • 带*标识的表示支持多次调用
连贯操作作用支持的参数类型
where*用于 AND 查询字符串、数组和对象
table用于定义要操作的数据表名称字符串和数组
name用于定义要操作的数据表名称字符串
field*用于定义要查询的字段(支持字段排除)字符串和数组
order*用于对结果排序字符串和数组
limit用于限制查询结果数量字符串和数字
page用于查询分页(内部会转换成 limit)字符串和数字
getLastSql获取上次执行的 sql 语句
fetchSql直接返回当前的 SQL 而不执行

# 一、表达式查询

  • 表达式是 SQL 语句的条件
  • 表达式不分大小写
  • 表达式写在where
表达式含义查询方法
=等于
<>不等于
>大于
>=大于等于
<小于
<=小于等于
[NOT] LIKE模糊查询whereLike/whereNotLike
[NOT] BETWEEN(不在)区间查询whereBetween/whereNotBetween
[NOT] IN(不在)IN 查询whereIn/whereNotIn
[NOT] NULL查询字段是否(不)是 NULLwhereNull/whereNotNull

# 1、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、tablename

# 必须完整数据库名
$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','asc')
            ->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');
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[<              
Name:
<提交>