Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

User1980's avatar

DB raw query issue - cannot get count

Hi,

I have this query:

        $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'

Thanks!

0 likes
15 replies
tykus's avatar

Use CASE statements:

$users = DB::table('users')
	->leftjoin('games', 'games.id', '=', 'games.user_id')
	->selectRaw("
		SUM(CASE WHEN games.status = 'active' THEN 1 ELSE 0 END) AS active_games,
		SUM(CASE WHEN games.status = 'pending' THEN 1 ELSE 0 END) AS pending_games
	")->orderBy($search, $order)
            ->paginate(100);
User1980's avatar

I tried this:

->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)")
)

tykus's avatar

But it fails

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.

User1980's avatar

Sorry:

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)
tykus's avatar
tykus
Best Answer
Level 104

Why did you add an extra closing parenthesis? There is one opening paren; and two closing parens in your code:

SUM(CASE status WHEN 'pending' THEN 1 ELSE NULL END) AS pending_games)

The original query I provided should work, don't change it.

User1980's avatar

Very well spotted, thank you so much!

User1980's avatar

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.

User1980's avatar

I tried this already:

->withCount([
    'sales as completed_sales_count' => function ($query) {
        $query->where('type', 'completed');
    },
    'sales as pending_sales_count' => function ($query) {
        $query->where('type', 'pending');
    }
])

But it is telling me that it is not in use Illuminate\Support\Facades\DB;

User1980's avatar

This looks like it works only for Eloquent and not DB query

MichalOravec's avatar

Just use eloquent.

$users = User::withCount([
    'sales as completed_sales_count' => function ($query) {
        $query->where('type', 'completed');
    },
    'sales as pending_sales_count' => function ($query) {
        $query->where('type', 'pending');
    }
])->select('users.*')
    ->leftjoin('games', 'users.id', '=', 'games.user_id')
    ->orderBy($search, $order)
    ->paginate(100);
User1980's avatar

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.

MichalOravec's avatar

I like when people don't provide a full query and they want to help. Bye.

User1980's avatar

I simply removed all the relations but the query is the same.....in the past I pasted long queries and got shouted at by other users.

Please or to participate in this conversation.