laravel在groupBy之后使用计数/求和方法 [英] laravel use count/sum method after groupBy

查看:239
本文介绍了laravel在groupBy之后使用计数/求和方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 count sum 方法时,我得到了错误的结果.

When I use count or sum method, I get the wrong result.

例如:

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

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

I only get result 1, in another method

Member::count()

我得到正确的结果.

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

I get wrong result when use sum method.

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

忘记说了,我的laravel版本信息:

Forgot to say, My laravel version info:

Laravel Framework version 5.1.40 (LTS)



项目中的实际问题

我有一个充电日志表.

1.pay日志表

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支付记录表数据

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.description

在pay_log表中,1和2记录相同.因此,当要按实际充电成功时间过滤结果时,我只需要一条记录,因此我使用groupBy操作.

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.

我的错误操作

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

最后,解决它.

$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:如果使用雄辩的ORM

ps: if use Eloquent ORM

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



测试示例(放弃)



Test Example(discard

  1. 创建表sql

  1. create table sql

CREATE TABLE成员(id int(11)NOT NULL AUTO_INCREMENT,名称varchar(15)DEFAULT NULL,十进制金额(20,2)默认值'0.00',主键(id))ENGINE = InnoDB AUTO_INCREMENT = 4 DEFAULT CHARSET = utf8;

测试数据

INSERT INTO hc_members(id,名称,金额)VALUES(1,'aaa',10.00),(2,'bbb',10.00),(3,'ccc',10.00);

测试结果

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

我想要结果3

实际上它返回1 .


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

我要结果30.00

实际上它返回10.00



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

推荐答案

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

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.)

尝试运行此查询

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

它将返回每个(id)聚合的行数计数.这是当您调用 Member :: groupBy('id')-> count()并且laravel仅期待一行返回第一个集合的计数时执行的查询.

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,可以致电

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

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

与您的总和查询也返回10的原因相同.该查询返回具有相同名称"的组的金额"之和.

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'.

要获取所有金额的总和,只需致电,

To get the sum of all amounts, simply call,

Member::sum('amount');

这篇关于laravel在groupBy之后使用计数/求和方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆