Note that there are some explanatory texts on larger screens.

plurals
  1. POMYSQL Query CASE returning ELSE
    text
    copied!<p>basically on the following lines where I have the two cases, it keeps returning the ELSES even though track_id is matched in the driver stats table for that driver_id as that is what I am grouping the query by. If I am doing this wrong, then what I need is if there are no rows in the driver_stats table then it gets the value from the drivers table and the growth is set to 0. Any help is greatly appreciated.</p> <pre><code>Case s.value When track_id = (SELECT MAX(track_id) FROM driver_stats) Then s.value Else d.value End AS value, Case s.growth When track_id = (SELECT MAX(track_id) FROM driver_stats) Then s.growth Else 0 End AS growth, **//FULL QUERY** SELECT d.drivers_id, d.drivername, t.code, Case s.value When track_id = (SELECT MAX(track_id) FROM driver_stats) Then s.value Else d.value End AS value, Case s.growth When track_id = (SELECT MAX(track_id) FROM driver_stats) Then s.growth Else 0 End AS growth, SUM(IF(qual_pos = '1', 1,0)) AS poles, SUM(IF(race_pos &lt;= '3', 1,0)) AS podiums, SUM(IF(race_pos = '1', 1,0)) AS victories, COALESCE(SUM(overtakes),0) AS overtakes, SUM(Case When s.track_id = (SELECT MAX(track_id) FROM driver_stats) Then points Else 0 End) lastracepoints, COALESCE(SUM(points),0) AS points FROM drivers d Left Join driver_stats s On s.drivers_id = d.drivers_id Left Join teams t On t.team_id = d.team_id Group By d.drivers_id ORDER BY d.drivers_id </code></pre> <p>Just for knowledge the table driver_stats has a row for each driver_id and for each track_id.</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