首页 文章

Group_concat with Query builder laravel

提问于
浏览
0

你好! ,我有一个使用查询生成器在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子句,则GROUP GROUP列(MIN(),MAX(),COUNT(),...)的混合没有GROUP列是非法的(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.com_id = 2 limit 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没有运气 .

如果你能帮我解决一下,我将非常感激!

1 回答

  • 0

    这实际上是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

相关问题