2017-03-09 93 views
1

我試圖用laravel 5.3建立一個查詢。但是當我做這個查詢時,我得到這個錯誤。Laravel SQLSTATE [42000]:語法錯誤或訪問衝突:1055

錯誤:

SQLSTATE[42000]: Syntax error or access violation: 1055 'laravel.location.locationDate' isn't in GROUP BY (SQL: select count(*), locationDate from location where tagCode = 24930 and xLocation > -1 and xLocation < 194 and yLocation > 60 and yLocation < 190 and created_at > 2017-03-09 00:00:01 and created_at < 2017-03-09 23:59:59 group by DATE(locationDate), hour(locationDate))

通過,如果我複製的查詢,並嘗試在SQL運行其工作的方式。但我只是加上引號,以created_at像「2017年3月9日00:00:01」

這是我的代碼..

$zoneTime = DB::table('location') 
          ->select(DB::raw('count(*), locationDate')) 
          ->where('tagCode', $tagCode) 
          ->where('xLocation', '>', $workingZone->x1) 
          ->where('xLocation', '<', $workingZone->x3) 
          ->where('yLocation', '>', $workingZone->y3) 
          ->where('yLocation', '<', $workingZone->y1) 
          ->where('created_at', '>', $startDate) 
          ->where('created_at', '<', $endDate) 
          ->groupBy(DB::raw('DATE(locationDate)')) 
          ->groupBy(DB::raw('hour(locationDate)')) 
          ->get(); 

回答

1

你必須使用在同一領域按這樣的clausel:

$zoneTime = DB::table('location') 
         ->select(DB::raw('count(*), locationDate')) 
         ->where('tagCode', $tagCode) 
         ->where('xLocation', '>', $workingZone->x1) 
         ->where('xLocation', '<', $workingZone->x3) 
         ->where('yLocation', '>', $workingZone->y3) 
         ->where('yLocation', '<', $workingZone->y1) 
         ->where('created_at', '>', $startDate) 
         ->where('created_at', '<', $endDate) 
         ->groupBy(DB::raw('DATE(locationDate) as locationDate')) 
         ->groupBy(DB::raw('hour(locationDate)')) 
         ->get(); 
+0

我仍然getin同樣的錯誤。 –

1

我改變了嚴格=虛假的內部配置/數據庫及其工作。

+0

這是一個臨時解決方案,你可能應該更新你的查詢。 – AnthonyB

相關問題