ThinkPHP6.0 数据库链式操作

收藏445

阅读187520

更新时间2022-04-28

前言:

数据库提供的链式操作方法,可以有效的提高数据存取的代码清晰度和开发效率,并且支持所有的CURD操作。


ThinkPHP6 数据库链式操作

  • 数据库提供的链式操作方法,可以有效的提高数据存取的代码清晰度和开发效率,并且支持所有的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查询字段是否(不)是NULLwhereNull/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

科技资讯

更多

精选课程

更多
前端入门_HTML5
前端入门_HTML5

共29课时

61.7万人学习

CSS视频教程-玉女心经版
CSS视频教程-玉女心经版

共25课时

39.3万人学习

JavaScript极速入门_玉女心经系列
JavaScript极速入门_玉女心经系列

共43课时

70.9万人学习

独孤九贱(1)_HTML5视频教程
独孤九贱(1)_HTML5视频教程

共25课时

61.6万人学习

独孤九贱(2)_CSS视频教程
独孤九贱(2)_CSS视频教程

共22课时

23万人学习

独孤九贱(3)_JavaScript视频教程
独孤九贱(3)_JavaScript视频教程

共28课时

33.9万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时

125万人学习

关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号