简体   繁体   English

Laravel: Eloquent select 返回不同的结果

[英]Laravel: Eloquent select returning different results

I'm trying to understand how Eloquent select changes the results of a query.我试图了解 Eloquent select 如何更改查询结果。 For example:例如:

$startDate = Carbon::createFromFormat('Y-m-d H:i:s', '2023-01-26 00:00:00', 'America/Chicago')->timezone('UTC');
$endDate = Carbon::createFromFormat('Y-m-d H:i:s', '2023-01-26 23:59:59', 'America/Chicago')->timezone('UTC');
$data = Orders::where('canceled', 0)
    ->whereBetween('created_at', [$startDate->toDateTimeString(), $endDate->toDateTimeString()])
    ->where(function ($query) {
        $query->where('is_branding', 1)
            ->orWhere('is_premium_branding', 1);
        })
    ->get();

I have some other code running a foreach on this data to calculate and I end up with:我有一些其他代码在这个数据上运行 foreach 来计算,我最终得到:

{
"branding_order_items_count": 12,
"branding_order_items_sales": 799.98,
"branding_order_items_margin": 169.71,
"branding_order_items_margin_percent": 0
}

However, if I run the same query but with an added select and calculate through the same foreach loop, I get a different result:但是,如果我运行相同的查询但添加了 select 并通过相同的 foreach 循环进行计算,我会得到不同的结果:

$startDate = Carbon::createFromFormat('Y-m-d H:i:s', '2023-01-26 00:00:00', 'America/Chicago')->timezone('UTC');
$endDate = Carbon::createFromFormat('Y-m-d H:i:s', '2023-01-26 23:59:59', 'America/Chicago')->timezone('UTC');
$data = Orders::where('canceled', 0)
    ->whereBetween('created_at', [$startDate->toDateTimeString(), $endDate->toDateTimeString()])
    ->where(function ($query) {
        $query->where('is_branding', 1)
            ->orWhere('is_premium_branding', 1);
        })
    ->select('*', DB::raw("count(*) as count")) // <<<<<<< Added this
    ->get();

With that added select, I get the following:添加 select 后,我得到以下信息:

{
"branding_order_items_count": 11,
"branding_order_items_sales": 649.99,
"branding_order_items_margin": 142.12,
"branding_order_items_margin_percent": 0
}

The first result is the correct numbers, 12 items / 799.98, etc. So why does adding the select to the eloquent query return a different result, and how can I have it return the correct results while still using the select?第一个结果是正确的数字,12 项/799.98 等。那么为什么将 select 添加到 eloquent 查询会返回不同的结果,我如何才能在仍然使用 select 的同时返回正确的结果?

TIA TIA

I'm assuming this is using a MySQL database.我假设这是使用 MySQL 数据库。 PostgreSQL would throw a grouping error with that query. PostgreSQL 会引发该查询的分组错误。

The reason your results differ is most likely due to DB::raw("count(*) as count") .您的结果不同的原因很可能是由于DB::raw("count(*) as count") Without a GROUP BY clause, the results will only be 1 row of data.如果没有GROUP BY子句,结果将只有 1 行数据。

If your orders table looks like this:如果您的orders表如下所示:

id ID cancelled取消
1 1个 1 1个
2 2个 0 0
3 3个 0 0
4 4个 1 1个

SELECT * FROM orders WHERE cancelled = 0 will return SELECT * FROM orders WHERE cancelled = 0将返回

id ID cancelled取消
2 2个 0 0
3 3个 0 0

SELECT *, COUNT(*) AS count FROM orders WHERE cancelled = 0 will return SELECT *, COUNT(*) AS count FROM orders WHERE cancelled = 0将返回

id ID cancelled取消 count数数
2 2个 0 0 2 2个

SELECT *, COUNT(*) AS count FROM orders WHERE cancelled = 0 GROUP BY id will return SELECT *, COUNT(*) AS count FROM orders WHERE cancelled = 0 GROUP BY id将返回

id ID cancelled取消 count数数
2 2个 0 0 1 1个
3 3个 0 0 1 1个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM