gpt4 book ai didi

Issues with Laravel groupBy(关于Laravel组的问题By)

转载 作者:bug小助手 更新时间:2023-10-25 23:07:11 28 4
gpt4 key购买 nike



I am using Laravel 10.

我用的是拉威尔10号。


This is crm_details table:

这是CRM_DETAILS表:


crm_details table


I need to count each status,. Also I need to consider parent_token with the same value as a single row. That means 2 rows have same `parent_token' then It will consider only latest Output will be Yes: 2, No :2

我需要清点每一种状态。此外,我还需要考虑使用与单行相同的值的PARENT_TOKEN。这意味着有2行具有相同的‘Parent_Token’,则它将只考虑最新的输出将为是:2、否:2


$salesCounts = DB::table('crm_details')
->select( DB::raw('count(crm_details.status) as count'),'crm_details.parent_token')
->groupBy('crm_details.status', 'crm_details.parent_token')
->get();

dd($salesCounts);

But I am getting all the data, what is wrong?

但是我得到了所有的数据,有什么问题吗?


更多回答

Just an addition to the answer, if you use count(*) instead, your database will determine the best field to use for count instead. Which will improve performance (but just slight).

这只是对答案的补充,如果您使用COUNT(*),您的数据库将确定用于COUNT的最佳字段。这将提高性能(但只是轻微的)。

优秀答案推荐

You are almost there, try with:

你就快成功了,试试:


$salesCounts = DB::table('crm_details as c1')
->select('c1.status', DB::raw('count(*) as count'))
->leftJoin('crm_details as c2', function($join) {
$join->on('c1.parent_token', '=', 'c2.parent_token')
->where('c1.id', '<', 'c2.id');
})
->whereNull('c2.id')
->groupBy('c1.status')
->get();

dd($salesCounts);


In MySQL, the GROUP BY operation is used to group rows based on similar values in the specified columns. When you use GROUP BY on two columns, it combines rows where both of those columns have identical values across all rows.

在MySQL中,GROUP BY操作用于根据指定列中的相似值对行进行分组。当您对两列使用GROUP BY时,它会合并这两列在所有行中都具有相同值的行。


There are no other rows in your dataset where the combination of parent_token as 'FFF' and status as 'Yes' repeats. For instance, in the first row, parent_token is 'FFF' and status is 'Yes,' and there are no other rows in the table with this exact combination of values.

在您的数据集中,没有其他行的Parent_Token为‘Fff’和Status为‘Yes’的组合重复。例如,在第一行中,PARENT_TOKEN为‘fff’,STATUS为‘Yes’,并且表中没有其他行具有该精确的值组合。


更多回答

I am expecting results as Yes 2, No 2 . If 2 rows have same `parent_token' then It will consider only latest row

我期待的结果是是2,不是2。如果有两行具有相同的‘Parent_Token’,则它将仅考虑最新的行

@YadhuBabu Check updated answer, please.

@YadhuBabu请查看更新后的答案。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com