samedi 5 janvier 2019

Query Condition if nothing add 0

I have a Laravel 5.7 query

$captains = DB::table('pickup_results')
    ->select(DB::raw("playerID"),
        DB::raw("COUNT(CASE WHEN gameResult = 'Win'  THEN 1 END) AS wins"),
        DB::raw("COUNT(CASE WHEN gameResult = 'Loss' THEN 1 END) AS loss"))
    ->where('pickOrder', '=', 0)
    ->where('playerID', '=', $playerID)
    ->groupBy('playerID')
    ->orderBy('wins','DESC')
    ->get();

If the playerID doesn't have a "Win" or a "Loss" it wont add the key of wins or loss to the query results -- thus resulting in errors when a user has no wins or no loss's in the database.

I Would like to be able to default wins or loss to 0 when a value doesn't exist in the database.

Can someone help me understand how I would achieve?



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2RBgX6I
via IFTTT

Aucun commentaire:

Enregistrer un commentaire