laravel 在 groupBy 之后使用 count/sum 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38005224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:03:51  来源:igfitidea点击:

laravel use count/sum method after groupBy

phplaravelcountgroup-bysum

提问by enbaby

When I use countor summethod, I get the wrong result.

当我使用countsum方法时,我得到错误的结果。

For example:

例如:

Member::groupBy('id')->count()

I only get result 1, in another method

我只得到结果 1,用另一种方法

Member::count()

I get the right result.

我得到了正确的结果。

I get wrong result when use summethod.

使用sum方法时我得到错误的结果。

https://github.com/laravel/framework/issues/14123

https://github.com/laravel/framework/issues/14123

Forgot to say, My laravel version info:

忘了说,我的 Laravel 版本信息:

Laravel Framework version 5.1.40 (LTS)



Actually problem in project



项目中的实际问题

I have a recharge log table.

我有一个 充值日志表

1.pay log table

1.支付日志表

CREATE TABLE pay_logs (
   id int(11) NOT NULL AUTO_INCREMENT,
   amount decimal(20,2) DEFAULT '0.00',
   pay_sn varchar(20) DEFAULT NULL,
   join_date int(11) unsigned DEFAULT '0',
   PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1.1 pay log talbe data

1.1 支付日志表数据

INSERT INTO pay_logs (id, amount, pay_sn, join_date)
VALUES
  (1,10.00,'aabbcc',1466753291),
  (2,10.00,'aabbcc',1466753292),
  (3,20.00,'bbccdd',1466753292),
  (4,30.00,'ccddee',1466753292);

2.description

2.说明

In pay_log table, the 1 and 2 record is same. So when wants to filter the results by the actually recharge success time, I just need one record, so I use groupBy operation.

在pay_log表中,1和2记录是一样的。所以当想通过实际充值成功时间过滤结果时,我只需要一条记录,所以我使用groupBy操作。

My wrong operation

我的错误操作

DB::table('pay_log')
 ->whereBetween('joinDate',[$beginToday,$endToday])
 ->where('isSucceed','=',1)
 ->where('type','=',1)
 ->groupBy('pay_sn');
 ->count();

Finally,solve it.

最后,解决它。

$query = DB::table('pay_log')
 ->select(DB::raw('count(*) as num'))
 ->whereBetween('joinDate',[$beginToday,$endToday])
 ->where('isSucceed','=',1)
 ->where('type','=',1)
 ->groupBy('pay_sn');

ps: if use Eloquent ORM

ps:如果使用 Eloquent ORM

modify ->mergeBindings($query)to ->mergeBindings($query->getQuery())

修改->mergeBindings($query)->mergeBindings($query->getQuery())



Test Example(discard



测试例(丢弃

  1. create table sql

    CREATE TABLE members ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(15) DEFAULT NULL, amount decimal(20,2) DEFAULT '0.00', PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

  2. test data

    INSERT INTO hc_members (id, name, amount) VALUES (1,'aaa',10.00), (2,'bbb',10.00), (3,'ccc',10.00);

  3. test result

  1. 创建表sql

    CREATE TABLE members ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(15) DEFAULT NULL, amount decimal(20,2) DEFAULT '0.00', PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

  2. 测试数据

    INSERT INTO hc_members (id, name, amount) VALUES (1,'aaa',10.00), (2,'bbb',10.00), (3,'ccc',10.00);

  3. 测试结果

In my hope, when I use the method Member::groupBy('name')->count()

我希望,当我使用该方法时 Member::groupBy('name')->count()

I want result 3

我想要结果 3

actually it returns 1.

实际上它返回 1


when I use the method Member::groupBy('name')->sum('amount')


当我使用该方法时 Member::groupBy('name')->sum('amount')

I want result 30.00

我想要结果 30.00

actually it returns 10.00

实际上它返回 10.00



Thanks for your answer to help me solve the problem!



感谢您的回答帮助我解决问题!

采纳答案by Rahul Govind

That's not an issue with laravel because Member::groupBy('id')->count()will return the count of number of results for each id. (Laravel is returning only the first row from the query result.)

这不是 laravel 的问题,因为Member::groupBy('id')->count()会返回每个 id 的结果数。(Laravel 只返回查询结果的第一行。)

Try running this query

尝试运行此查询

SELECT COUNT(*) AS aggregate FROM members GROUP BY 'id'

It will return a count of the number of rows for each (id) aggregate. This is the query executed when you call Member::groupBy('id')->count()and laravel, expecting only one row, returns the count of the first aggregate.

它将返回每个 (id) 聚合的行数计数。这是当您调用Member::groupBy('id')->count()laravel时执行的查询,期望只有一行,返回第一个聚合的计数。

If you want a count of the distinct ids, you can call

如果你想要一个不同 id 的计数,你可以调用

Member::distinct('id')->count('id')

This is the same reason your sum query also returns 10. The query returns the sum of 'amount' for a group with the same 'name'.

这与您的 sum 查询也返回 10 的原因相同。该查询返回具有相同“名称”的组的“金额”总和。

To get the sum of all amounts, simply call,

要获得所有金额的总和,只需调用,

Member::sum('amount');