【问题标题】:Group_concat with Query builder laravelGroup_concat 与查询生成器 laravel
【发布时间】:2018-05-20 15:16:39
【问题描述】:

你好! ,我在使用查询生成器的 laravel 5.5 上调用 sql 时遇到问题。当我这样做时

    $result = DB::table(self::$TABLA_COMPONENTE)
        ->join(self::$TABLA_ARCHIVOS ,self::$TABLA_COMPONENTE.'.com_id','=',self::$TABLA_ARCHIVOS.'.com_id')
        ->select(self::$TABLA_COMPONENTE.'.*',DB::raw('group_concat('.self::$TABLA_ARCHIVOS.'.ar_url) as com_archivos'))
        ->where(self::$TABLA_COMPONENTE.'.com_id',$id)->first();

我收到以下错误

SQLSTATE[42000]:语法错误或访问冲突:1140 如果没有 GROUP BY 子句( SQL: select componente.*, group_concat(archivos.ar_url) as com_archivos from componente inner join archivos on componente.com_id = archivos.com_id where componente.@987 = 2 限制 1)

这是我使用 ->toSql() 得到的原始 sql

This is the sql with ->toSql()

"select `componente`.*, group_concat(archivos.ar_url) as com_archivos from `componente` inner join `archivos` on `componente`.`com_id` = `archivos`.`com_id` where `componente`.`com_id` = ?

它在 Phpmyadmin 上运行良好。

我也尝试过使用 Group by,但没有成功。

如果您能帮助我解决问题,我将不胜感激!

【问题讨论】:

    标签: sql laravel mariadb query-builder laravel-5.5


    【解决方案1】:

    这实际上是 mysql 问题而不是 laravel 问题。尝试添加

    ->groupBy(self::$TABLA_COMPONENTE . '.id')
    

    给你查询。

    已编辑:类似这样的内容:

    $result = DB::table(self::$TABLA_COMPONENTE)
        ->join(self::$TABLA_ARCHIVOS ,self::$TABLA_COMPONENTE.'.com_id','=',self::$TABLA_ARCHIVOS.'.com_id')
        ->select(self::$TABLA_COMPONENTE.'.*',DB::raw('group_concat('.self::$TABLA_ARCHIVOS.'.ar_url) as com_archivos'))
        ->groupBy(self::$TABLA_COMPONENTE . '.id')
        ->where(self::$TABLA_COMPONENTE.'.com_id',$id)->first();
    

    注意:它不是 testet

    【讨论】:

    • 感谢您的回复,但它也不起作用! ->groupBy(self::$TABLA_COMPONENTE . '.com_id') SQLSTATE[42000]: 语法错误或访问冲突:1055 'bd_empresa.componente.com_nombre' 不在 GROUP BY 中(SQL:选择 componente.*, group_concat(archivos.ar_url) as com_archivos from componente inner join archivos on componente.com_id = archivos.com_id 其中componente.com_id = 5432.@@9876 分组987654333@ 限制 1)
    猜你喜欢
    • 2014-05-13
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    • 2016-01-10
    • 2019-01-29
    • 2018-11-27
    相关资源
    最近更新 更多