Note that there are some explanatory texts on larger screens.

plurals
  1. POFetch count based on 2 different values in same table
    text
    copied!<p>I'm trying to fetch statistics to an user for a game application but need help with making it to work with out having to make more then one query.</p> <p>This is how it looks at the moment in short: Table: userid(X int), outcome(win and loss), race (A, B and C)</p> <p>I need now to fetch the count of win and loss for each race:</p> <pre><code>Select count(outcome) as win from games where userid = X and outcome = 'win' AND race = A Select count(outcome) as loss from games where userid = X and outcome = 'loss' AND race = A Select count(outcome) as win from games where userid = X and outcome = 'win' AND race = B Select count(outcome) as loss from games where userid = X and outcome = 'loss' AND race = B Select count(outcome) as win from games where userid = X and outcome = 'win' AND race = C Select count(outcome) as loss from games where userid = X and outcome = 'loss' AND race = C </code></pre> <p>Then i basically calculates them in php after that but this is ridiculously slow approach when a large number of games is in the database.</p> <p>So i basically want to have an query that groups by race like this:</p> <p>Race | Win | Loss</p> <p>A_<strong><em>_</em>___</strong><em>x</em><strong><em>_</em>___</strong>_x</p> <p>B_<strong><em>_</em>___</strong><em>x</em><strong><em>_</em>___</strong>_x</p> <p>C_<strong><em>_</em>___</strong><em>x</em><strong><em>_</em>___</strong>_x</p> <p>I'm quite new to more complex forms of SQL so any suggestions on optimize this query would be helpful. </p> <p>Thank you. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload