TP5 常用方法

简介: TP5 常用方法

TP5 常用方法

1. where
$where = [
    'name' => 'Tom',
    'age' => ['>', 20]
];
$result = Db::table('users')
          ->where($where)
          ->select();
// 等价于
$result = Db::table('users')
          ->where('name', 'Tom')
          ->where('age', '>', 20)
          ->select();
2. whereRaw
$result = Db::table('users')
          ->whereRaw('age > ? and status = ?', [20, 1])
          ->select();
// 等价于
$result = Db::table('users')
          ->where('age', '>', 20)
          ->where('status', '=', 1)
          ->select();
3. whereBetween
$result = Db::table('users')
          ->whereBetween('age', [20, 30])
          ->select();
4. whereNull
$result = Db::table('users')
          ->whereNull('email')
          ->select();
// 等价于
$result = Db::table('users')
          ->where('email', 'NULL')
          ->select();
5. whereIn
$result = Db::table('users') 
        ->whereIn('name', ['Tom', 'Jerry', 'Mike']) 
        ->select();
6. page
// 该方法用于指定查询结果的分页条件,其参数为一个数字表示当前页数。
$result = Db::table('users')
          ->page(2)
          ->select();
// 等价于
$result = Db::table('users')
          ->limit(10, 10)
          ->select();
7. having
// 查询年龄大于 20 的用户,且他们拥有的订单数不少于 10
// 该方法用于添加 HAVING 子句,与 WHERE 类似,但是主要用于聚合查询
$result = Db::table('users')
          ->join('orders', 'users.id=orders.user_id', 'LEFT')
          ->where('age', '>', 20)
          ->group('users.id')
          ->having('count(orders.id)', '>=', 10)
          ->select();
8. group
// 查询每个城市的用户数和平均年龄
$result = Db::table('users')
          ->field('city, count(id) as count, avg(age) as avg_age')
          ->group('city')
          ->select();
9. 模糊查询
// TODO 1 使用 `where` 方法的回调函数来添加多个查询条件。该回调函数会接收一个 `$query` 对象参数,可以在其中调用多个 `where` 方法实现多条件查询。需要注意的是,多个 `where` 方法之间的关系是 **AND** 关系,即所有条件都要满足。最后调用 `select` 方法执行查询,并返回结果数组。
$query = Db::table('user') // 指定查询的表
    ->where(function ($query) {
        $query->where('name', 'like', '张%') // 姓张
              ->where('phone', 'like', '156%') // 手机号以156开头
              ->where('gender', '=', '男') // 性别是男
              ->where('birthday', '>=', '2002-04-07 00:00:00') // 出生日期大于等于2002年4月7日
              ->where('birthday', '<', '2002-04-08 00:00:00'); // 出生日期小于2002年4月8日
    });
$data = $query->select(); // 执行查询操作,并返回结果数组
// TODO 2 先使用 `where` 方法的回调函数将多个查询条件组合成一个 `$where` 对象。然后在主查询中直接传入 `$where` 对象,一次性实现多条件查询。最后调用 `select` 方法执行查询,并返回结果数组。此时所有条件之间的关系也是 **AND** 关系,即所有条件都要满足
$where = function ($query) {
    $query->where('name', 'like', '张%') // 姓张
          ->where('phone', 'like', '156%') // 手机号以156开头
          ->where('gender', '=', '男') // 性别是男
          ->where('birthday', '>=', '2002-04-07 00:00:00') // 出生日期大于等于2002年4月7日
          ->where('birthday', '<', '2002-04-08 00:00:00'); // 出生日期小于2002年4月8日
};
$query = Db::table('user') ->where($where); // 将所有条件组合成一个 $where 对象,再一次性传入主查询中
$data = $query->select(); // 执行查询操作,并返回结果数组
// TODO 3 使用 `$where` 数组将多个查询条件存储起来。数组中的每个键值对分别代表一个查询条件,其键为查询字段名,其值为一个数组,其中第一个元素为比较运算符,第二个元素为比较的值。最后在主查询中直接传入 `$where` 数组,一次性实现多条件查询。需要注意的是,如果同一个字段有多个查询条件,后面的会覆盖前面的。最后调用 `select` 方法执行查询,并返回结果数组。
$where = [
    'name' => '张%', // 姓张
    'phone' => '156%', // 手机号以156开头
    'gender' => '男', // 性别是男
    'birthday' => ['>=', '2002-04-07 00:00:00'], // 出生日期大于等于2002年4月7日
    'birthday' => ['<', '2002-04-08 00:00:00'], // 出生日期小于2002年4月8日
];
$query = Db::table('user') ->where($where); // 直接将 $where 数组作为参数传入 where 方法中
$data = $query->select(); // 执行查询操作,并返回结果数组
相关文章
|
2月前
|
C# 索引
C#学习相关系列之base和this的常用方法
C#学习相关系列之base和this的常用方法
如何使用Stream流将List转换为Map
如何使用Stream流将List转换为Map
|
2月前
使用Lamda表达式、stream流遍历Map、list
使用Lamda表达式、stream流遍历Map、list
Python应用专题 | 1:如何根据mask list提取目标list中元素
介绍Python在具体任务中使用:如何根据mask list提取目标list中元素
|
7月前
|
Java
mp对象集合in查询
mp对象集合in查询
44 1
|
8月前
jdk8 Stream流中将集合转成map,重复key处理,统计最大值,获取某个属性集合等10种最常用方法
jdk8 Stream流中将集合转成map,重复key处理,统计最大值,获取某个属性集合等10种最常用方法
121 5
|
11月前
|
中间件 PHP
tp5与tp6的区别--详解
tp5与tp6的区别--详解
229 0
|
12月前
|
Java
java – 为什么stream average()方法返回OptionalDouble而不是double?
java8的函数式编程中中提供了Optional 类型, 旨在帮助用户规避NEP, 如果stram中的值都为0
73 0
|
IDE 编译器 API
stream的实用方法和注意事项
相信大家一定都在项目开发中享受过stream带来的便利性和优雅的代码风格。接下来补充几个项目中不常见到但是同样实用的api,同时跟大家一起探讨stream这把双刃剑的另一面。
190 0
stream的实用方法和注意事项
|
Java
List Stream 的常规用法
List Stream 的常规用法
101 0