MYSQL Query with IF statements
having a little trouble, how could I alter this mysql query so that if there are rows in the member_results table it will run the query below but if there is none then pretty much run the query below just without the SUMS() and the joins with the member_results table. I am guessing it can be done with the IF statement in mysql I just have no clue really how to implement with the query below.
Any help is much appreciated.
$result = mysql_query(" SELECT m.member_id, m.teamname,
Sum(Case When r.track_id = '$chosentrack' -1 AN开发者_如何学编程D r.track_id >= l.start_race
Then total_points Else 0 End) LastRacePoints,
Sum(Case When r.track_id <= '$chosentrack' -1 AND r.track_id >= l.start_race
Then total_points Else 0 End) TotalPoints
FROM members m
Join members_leagues l
On l.member_id = m.member_id
Join member_results r
On r.member_id = m.member_id
Where l.league_id = '$chosenleague'
Group By m.member_id
Order By TotalPoints Desc, LastRacePoints DESC, m.teamname Desc ")
or die ("Error - could not display league");
Just replace Join member_results r
with Left Join member_results r
and it should work. You should get NULLs for the sums if there are no results.
精彩评论