Laravel Query Builder 在按 ID 分组的查询中按子查询减去 COUNT

Posted

技术标签:

【中文标题】Laravel Query Builder 在按 ID 分组的查询中按子查询减去 COUNT【英文标题】:Laravel Query Builder Subtract COUNT by Subquery in query grouped by ID 【发布时间】:2020-06-04 14:35:53 【问题描述】:

我陷入了在 Laravel 查询中遇到的这个问题。

首先,我使用这个查询来获取交易

$transactions = Transaction::select(
    'customers.member_id as member_id',
    DB::raw('
        CASE
            WHEN transactions.customer_id IS NOT NULL
                THEN
                    customers.name 
            ELSE 
                "Tanpa pelanggan" 
        END AS customer_name
    '),
    'customers.phone as customer_phone',
    DB::raw('COUNT(transactions.id) as sum_qty'),
    DB::raw('SUM(final_amount) as sum_amount')
)
->join('outlets', 'outlets.id', '=', 'transactions.outlet_id')
->leftJoin('customers', 'customers.id', '=', 'transactions.customer_id')
->where('outlets.company_id', '=', $companyID)
->where(function ($q) use ($permittedOutlets) 
    if (!empty($permittedOutlets)) 
        $q->whereIn('transactions.outlet_id', $permittedOutlets);
    
)
->where(function ($query) use ($companyID, $outletID, $startDate, $endDate, $filterData) 
    if ($outletID != 0) 
        $query->where('transactions.outlet_id', '=', $outletID);
    
    if (!empty($startDate) && !empty($endDate)) 
        if ($startDate == $endDate) 
            $query->whereDate(deviceTimestamp(), '=', $startDate);
         else 
            $query->whereDate(deviceTimestamp(), '>=', $startDate)
                ->whereDate(deviceTimestamp(), '<=', $endDate);
        
    
    if ($filterData != "") 
        $query->where(function ($q) use ($filterData) 
            $q->where('customers.name', 'like', '%' . $filterData . '%')
                ->orWhere('customers.phone', 'like', '%' . $filterData . '%');
        );
    
)
->isSuccessAndVoidedInDifferentDate()
->orderBy($sortField, $sort)
->groupBy('transactions.customer_id');

对于 isSuccessAndVoidedInDifferentDate 交易模型范围

$query->whereRaw('
    CASE 
        WHEN transactions.void_id IS NOT NULL
            THEN 
                EXISTS (
                    SELECT *
                    FROM transactions as transactions2
                    WHERE transactions.void_id = transactions2.id
                    AND transactions.created_at <> transactions2.created_at
                    LIMIT 1
                )                
        WHEN transactions.status = \''.self::STATUS_VOID.'\'
            THEN 
                EXISTS (
                    SELECT *
                    FROM transactions as transactions2
                    WHERE transactions.id = transactions2.void_id
                    AND transactions.created_at <> transactions2.created_at
                    LIMIT 1
                )   
        ELSE 
                TRUE
    END
');

对于原始 mysql 查询

SELECT 
    `customers`.`member_id` AS `member_id`,
    CASE
        WHEN transactions.customer_id IS NOT NULL THEN customers.name
        ELSE 'Tanpa pelanggan'
    END AS customer_name,
    `customers`.`phone` AS `customer_phone`,
    COUNT(transactions.id) AS sum_qty,
    SUM(final_amount) AS sum_amount
FROM
    `transactions`
        INNER JOIN
    `outlets` ON `outlets`.`id` = `transactions`.`outlet_id`
        LEFT JOIN
    `customers` ON `customers`.`id` = `transactions`.`customer_id`
WHERE
    `outlets`.`company_id` = 153113
        AND (`transactions`.`outlet_id` IN (9164))
        AND (DATE(ADDDATE(transactions.device_timestamp,
            INTERVAL transactions.timezone HOUR)) >= '2020-02-01'
        AND DATE(ADDDATE(transactions.device_timestamp,
            INTERVAL transactions.timezone HOUR)) <= '2020-02-29')
        AND CASE
        WHEN
            transactions.void_id IS NOT NULL
        THEN
            EXISTS( SELECT 
                    *
                FROM
                    transactions AS transactions2
                WHERE
                    transactions.void_id = transactions2.id
                        AND transactions.created_at <> transactions2.created_at
                LIMIT 1)
        WHEN
            transactions.status = 'void'
        THEN
            EXISTS( SELECT 
                    *
                FROM
                    transactions AS transactions2
                WHERE
                    transactions.id = transactions2.void_id
                        AND transactions.created_at <> transactions2.created_at
                LIMIT 1)
        ELSE TRUE
    END
        AND `transactions`.`deleted_at` IS NULL
GROUP BY `transactions.customer_id`
ORDER BY `transactions.customer_id` ASC

所有交易都有两种不同的状态。状态为成功或无效。交易类型分为两类。第一类是有客户的交易,第二类是没有客户的交易。在这种情况下,没有客户的交易将被选为“Tanpa pelanggan”。这里的所有交易都按 transactions.customer_id 分组。

这个问题的重点是

   DB::raw('COUNT(transactions.id) as sum_qty'),

在选择部分。为了更容易理解这里的公司、网点和日期的where子句可以忽略。我正在尝试创建一个条件,其中查询中的计数将返回成功交易的计数减去按 transactions.customer_id 分组的无效交易的计数。我有点困惑是否应该使用与主查询相同的 where 条件进行子查询,这有点重复或我没有找到任何更好的解决方案。

示例: 数据包括:

1 笔无效交易 其余交易成功

预期结果:

member_id | customer_name | customer_phone| sum_qty | sum_ammount

NULL      | Sam           | 123xxxx       | 1       | 20720

NULL      | Tanpa Pelanggan| -            | 2       | 28490

实际结果:

member_id | customer_name | customer_phone| sum_qty | sum_ammount

NULL      | Sam           | 123xxxx       | 1       | 20720

NULL      | Tanpa Pelanggan| -            | 3       | 51800

我的问题是解决这个问题的最佳方法是什么?

提前感谢您!

【问题讨论】:

【参考方案1】:

找到了解决办法!

DB::raw('SUM(CASE WHEN transactions.status != \'void\' THEN 1 ELSE 0 END) - SUM(CASE WHEN transactions.status = \'void\' THEN 1 ELSE 0 END) as sum_qty')

就是实际使用SUM,在SUM函数里面加上CASE条件。

【讨论】:

以上是关于Laravel Query Builder 在按 ID 分组的查询中按子查询减去 COUNT的主要内容,如果未能解决你的问题,请参考以下文章

Laravel中的Query Builder

Laravel中的Query Builder

Laravel:调用未定义的方法 Illuminate\\Database\\Query\\Builder

laravel 5.2 调用未定义的方法 Illuminate\Database\Query\Builder::associate()

“使用 Laravel 调用未定义的方法 Illuminate\\Database\\Query\\Builder::users() [关闭]

Laravel:在“query-builder”或“eloquent”中更改原始查询