
在laravel开发中,我们经常需要与数据库进行交互。虽然直接使用原始sql查询(db::raw() 或 db::select())在某些复杂场景下显得直接有效,但它牺牲了laravel查询构建器提供的诸多便利,如参数绑定带来的安全性、链式调用带来的可读性以及跨数据库的兼容性。尤其是当查询中包含子查询时,将原始sql转换为查询构建器表达式可能会让一些开发者感到困惑。本教程将以一个典型的嵌套查询为例,详细讲解如何利用laravel的fromsub方法优雅地实现这一转换。
我们首先来看一个包含子查询的原始SQL查询,它旨在计算特定商店员工的订单总价:
SELECT inventory.EmployeeID,
inventory.created_date AS OrderDate,
SUM(inventory.calculation) AS TotalPrice
FROM (
SELECT i.id AS ItemID,
o.id AS OrderID,
o.EmployeeID,
o.created_date,
(o.Quantity * i.price) AS calculation
FROM `stationary_orders` AS o
LEFT JOIN `stationary_items` AS i ON o.Stationary_ID = i.id
WHERE o.Store IN $storess
ORDER BY o.id DESC
LIMIT $Limit,10
) AS inventory
GROUP BY inventory.EmployeeID;这个查询的核心是一个子查询(FROM (...) AS inventory),它首先从 stationary_orders 和 stationary_items 表中获取订单明细,计算每项的价值(calculation),并对结果进行过滤、排序和分页。然后,外部查询基于这个子查询的结果(inventory)按 EmployeeID 分组,计算每个员工的订单总价。
Laravel查询构建器提供了fromSub方法,专门用于处理FROM子句中的子查询。它的基本用法是:-youjiankuohaophpcnfromSub(function ($query) { ... }, 'alias_name')。
下面是将上述原始SQL转换为Laravel查询构建器表达式的步骤和代码:
首先,我们需要将原始SQL中的内部子查询部分转换为Laravel查询构建器表达式。这部分查询负责计算每项的价值并进行初步筛选。
use Illuminate\Support\Facades\DB;
// 假设 $stores 和 $limitOffset 是从外部传入的变量
$stores = [1, 2, 3]; // 示例商店ID数组
$limit = 0; // 示例偏移量
$pageSize = 10; // 示例每页数量
$nestedQuery = DB::table('stationary_orders', 'o') // 使用 'o' 作为 orders 表的别名
->select(
'i.id AS ItemID',
'o.id AS OrderID',
'o.EmployeeID',
'o.created_date',
DB::raw('(o.Quantity * i.price) AS calculation') // 计算字段使用 DB::raw
)
->leftJoin('stationary_items AS i', 'o.Stationary_ID', '=', 'i.id') // 使用 'i' 作为 items 表的别名
->whereIn('o.Store', $stores) // 使用 whereIn 处理 IN 子句
->orderBy('o.id', 'DESC')
->offset($limit) // 对应 LIMIT $Limit
->limit($pageSize); // 对应 ,10代码解释:
接下来,我们将上述构建好的 $nestedQuery 作为子查询,通过 fromSub 方法嵌入到主查询的 FROM 子句中。
$result = DB::query() // 可以使用 DB::query() 或 DB::table('') 开始一个新查询
->fromSub($nestedQuery, 'inventory') // 将 $nestedQuery 作为子查询,并命名为 'inventory'
->select(
'inventory.EmployeeID',
'inventory.created_date AS OrderDate',
DB::raw('SUM(inventory.calculation) AS TotalPrice') // 再次使用 DB::raw() 处理聚合函数
)
->groupBy('inventory.EmployeeID')
->get(); // 执行查询并获取结果
// $result 将是一个 Illuminate\Support\Collection 对象,其中包含查询结果代码解释:
将上述两部分结合起来,完整的Laravel查询构建器代码如下:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class OrderController extends Controller
{
public function getEmployeeOrderSummary(Request $request)
{
// 从请求中获取或设置默认值
$stores = $request->input('stores', [1, 2, 3]); // 示例:从请求中获取商店ID数组
$limit = $request->input('offset', 0); // 示例:从请求中获取偏移量
$pageSize = $request->input('limit', 10); // 示例:从请求中获取每页数量
// 1. 构建内部子查询
$nestedQuery = DB::table('stationary_orders', 'o')
->select(
'i.id AS ItemID',
'o.id AS OrderID',
'o.EmployeeID',
'o.created_date',
DB::raw('(o.Quantity * i.price) AS calculation')
)
->leftJoin('stationary_items AS i', 'o.Stationary_ID', '=', 'i.id')
->whereIn('o.Store', $stores)
->orderBy('o.id', 'DESC')
->offset($limit)
->limit($pageSize);
// 2. 将子查询嵌入到主查询中
$employeeOrderSummary = DB::query()
->fromSub($nestedQuery, 'inventory')
->select(
'inventory.EmployeeID',
'inventory.created_date AS OrderDate',
DB::raw('SUM(inventory.calculation) AS TotalPrice')
)
->groupBy('inventory.EmployeeID')
->get();
return response()->json($employeeOrderSummary);
}
}通过本教程,我们详细演示了如何利用Laravel查询构建器的 fromSub 方法,将复杂的原始SQL子查询转换为更具Laravel风格的、安全且易于维护的代码。掌握这一技巧,将帮助开发者更有效地利用Laravel的强大功能,构建出高质量的数据库交互逻辑。在处理嵌套查询时,记住分解问题、逐步构建子查询,并最终将其集成到主查询中,将使整个过程变得更加清晰和可控。
以上就是如何将原始SQL子查询转换为Laravel查询构建器表达式的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号