'SQLSTATE[42000]: Syntax error or access violation: 1064
-
I have a query like this
SELECT tt.* FROM `laravel-sb`.`chats` tt INNER JOIN (SELECT chatroom_id, MAX(created_at) AS MaxDateTime FROM `laravel-sb`.`chats` WHERE `receiver_id`=7 GROUP BY chatroom_id) groupedtt ON tt.chatroom_id = groupedtt.chatroom_id AND tt.created_at = groupedtt.MaxDateTime WHERE `receiver_id`=7 -- and `sender_id` = 1 Order By Not isNull( seen_at ), created_at DESC, millisecond DESC
I try to put it into laravel like this
$query = DB::select( DB::raw('select tt.* FROM `chats` tt INNER JOIN (SELECT chatroom_id, MAX(created_at) AS MaxDateTime FROM `chats` WHERE `receiver_id`= ' . $profileId . ' GROUP BY chatroom_id) groupedtt ON tt.chatroom_id = groupedtt.chatroom_id AND tt.created_at = groupedtt.MaxDateTime WHERE `receiver_id`= ' . $profileId . ' Order By Not isNull( seen_at ), created_at DESC, millisecond DESC ') )->get();
The query works in my sql editor, but not in laravel.
I get the error:
Illuminate/Database/QueryException with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use nea