簡體   English   中英

使用查詢生成器laravel的Group_concat

[英]Group_concat with Query builder laravel

你好! ,我在使用查詢生成器的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(),...)混合是非法的(SQL:select componente 。*,GROUP_CONCAT(archivos.ar_url),如從com_archivos componente內部聯接archivoscomponentecom_id = archivoscom_id其中componentecom_id = 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,但沒有運氣。

如果您能為我提供解決方案,我將不勝感激!

這實際上是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();

注意:它不是睾丸

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM