$users = DB::table('users')
->leftjoin('games', 'games.id', '=', 'games.user_id')
->select(
DB::raw("count(games.*) AS active_games WHERE status='active'"),
DB::raw("count(games.*) AS pending_games WHERE status='pending'"),
)
->orderBy($search, $order)
->paginate(100);
I am trying to get the count of the active and pending games per user.
I need to keep this as a DB query and not eloquent as my query is very very long(I removed all the unnecessary code).
Any idea why the DB::raw query is bringing the error:
Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE status = 'pending'
->select(
DB::raw("COUNT(CASE status WHEN 'active' THEN 1 ELSE NULL END) AS active_games)"),
DB::raw("COUNT(CASE status WHEN 'pending' THEN 1 ELSE NULL END) AS pending_games)")
)
But it fails.
I also tried this:
->select(
DB::raw("SUM(CASE status WHEN 'active' THEN 1 ELSE NULL END) AS active_games)"),
DB::raw("SUM(CASE status WHEN 'pending' THEN 1 ELSE NULL END) AS pending_games)")
)
It does not give much information to work with; how does this fail? Also, I specifically used SUM because I want to SUM the 1s and 0s in the CASE statements.
Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '), COUNT(CASE status WHEN 'pending' THEN 1 ELSE NULL END) AS pending_games)
Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '), SUM(CASE status WHEN 'pending' THEN 1 ELSE NULL END) AS pending_games)
For how long have you been coding Tykus? You are so good! You find so many issues for so many people in all threads......Are you a Senior full-stack? Hat off.
I cannot, unfortunately. The query I am working on is massive and has been built long ago with DB query. To redo it all....ouch. The query deals with multiple search bars and about 8 table relations.