database 在 Laravel 中按多列分组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23185432/
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-08 09:13:45  来源:igfitidea点击:

Group by multiple columns in Laravel

databaselaravel

提问by satjan

How to GROUP BYmultiple column in Laravel? I tried this code:

如何GROUP BY在 Laravel 中多列?我试过这个代码:

$routes = DB::table('route')
    ->groupBy('rte_origin')
    ->groupBy('rte_destination')
    ->get();

But this is not working.

但这是行不通的。

回答by Gustavo Ramos

Did you try:

你试过了吗:

$routes = DB::table('route')
->groupBy('rte_origin', 'rte_destination')
->get();

Can't test here right now, but the API says groupBy() accepts an array.

现在不能在这里测试,但 API 说 groupBy() 接受一个数组。

For reference, please visit:

如需参考,请访问:

回答by Jon Watson

It is incorrect to believe that the Database\Query\Builder::groupBy() method accepts arrays as arguments. Currently, it will only accept N number of string arguments.

认为 Database\Query\Builder::groupBy() 方法接受数组作为参数是不正确的。目前,它只接受 N 个字符串参数。

As of this writing, the current version of the Laravel Framework is: v4.2.4, and the code for the Database\Query\Builder::groupBy() method is as follows:

在撰写本文时,Laravel Framework 的当前版本为:v4.2.4,Database\Query\Builder::groupBy() 方法的代码如下:

/**
 * Add a "group by" clause to the query.
 *
 * @param  dynamic  $columns
 * @return \Illuminate\Database\Query\Builder|static
 */
public function groupBy()
{
    $this->groups = array_merge((array) $this->groups, func_get_args());

    return $this;
}

If you think about it, func_get_args() returns an array of all columns that might have been fed in as strings. Therefore, expected input of this function is:

如果你仔细想想,func_get_args() 会返回一个包含所有可能作为字符串输入的列的数组。因此,该函数的预期输入为:

$builder->groupBy('column1', 'column2', ...);

The resulting $this->groups property on the builder object should be an array of strings, like this:

生成器对象上的 $this->groups 属性应该是一个字符串数组,如下所示:

['column1','column2']

However, if we fed an array into the method above, like this:

但是,如果我们将一个数组输入到上面的方法中,就像这样:

$builder->groupBy(['column1','column2']);

the $this->groups property would end up with a nested array that looks like this:

$this->groups 属性最终会得到一个如下所示的嵌套数组:

[['column1','column2']]

The rest of the query builder framework expects the $builder->groups property to be a non-nested array of strings. Thus, when the framework tries to columnize and wrap table names in their proper escaped format (each database engine has a different table name escape operator), it tries to wrap an array instead of a string and you get your error.

查询构建器框架的其余部分期望 $builder->groups 属性是一个非嵌套的字符串数组。因此,当框架尝试以正确的转义格式(每个数据库引擎具有不同的表名转义运算符)对表名称进行列化和包装时,它会尝试包装数组而不是字符串,并且您会收到错误消息。

The offending error line is line 49 in Database\Grammar::wrap().

有问题的错误行是 Database\Grammar::wrap() 中的第 49 行。

If we were to modify the Database\Query\Builder::groupBy() method to make it accept arrays, we would rewrite it something like the following:

如果我们要修改 Database\Query\Builder::groupBy() 方法以使其接受数组,我们会像下面这样重写它:

public function groupBy()
{
    $args = func_get_args();
    foreach($args AS $arg)
    {
        $arg = (is_array($arg)) ? $arg:[$arg];
        $this->groups = array_merge((array) $this->groups, $arg);
    }

    return $this;
}

This method accepts an array at any parameter index.

此方法接受任何参数索引处的数组。

回答by Lavlesh Mishra

Edit your applications's database config file config/database.php

编辑应用程序的数据库配置文件 config/database.php

In mysql array, set strict => falseto disable MySQL's strict mode to make this work.

在 mysql 数组中,设置strict => false为禁用 MySQL 的严格模式以使其工作。

回答by Abdoulaye SOW

Put the selectbefore the groupByand the arguments in brackets.

select前面groupBy和参数放在括号中。

$routes = DB::table('route')
    ->select(['column_1', 'column_2'])
    ->groupBy(['column_1', 'column_2'])
    ->get();