The count value only grabs 1 for each row

advertisements

I am making a stats page about golf for the people I play with. I am trying to pull out of the database the number of times out of all our scorecards that we received birdies (which is -1 under par). It does pull out the -1s per hole, however I noticed that you if you had 2 birdies on a scorecard, it still only counts as 1 birdie instead of 2. I want it to keep counting, so if someone gets 9 birdies, those 9 are added to the total.

$query_p321 = "SELECT t1.*,COUNT(t1.player_id),t2.* FROM scorecards t1 LEFT JOIN courses t2 ON t1.course_id=t2.course_id
WHERE t1.hole1<t2.hole1_par AND t1.hole1>t2.hole1_par-2
     OR t1.hole2<t2.hole2_par AND t1.hole2>t2.hole2_par-2
     OR t1.hole3<t2.hole3_par AND t1.hole3>t2.hole3_par-2
     OR t1.hole4<t2.hole4_par AND t1.hole4>t2.hole4_par-2
     OR t1.hole5<t2.hole5_par AND t1.hole5>t2.hole5_par-2
     OR t1.hole6<t2.hole6_par AND t1.hole6>t2.hole6_par-2
     OR t1.hole7<t2.hole7_par AND t1.hole7>t2.hole7_par-2
     OR t1.hole8<t2.hole8_par AND t1.hole8>t2.hole8_par-2
     OR t1.hole9<t2.hole9_par AND t1.hole9>t2.hole9_par-2
     OR t1.hole10<t2.hole10_par AND t1.hole10>t2.hole10_par-2
     OR t1.hole11<t2.hole11_par AND t1.hole11>t2.hole11_par-2
     OR t1.hole12<t2.hole12_par AND t1.hole12>t2.hole12_par-2
     OR t1.hole13<t2.hole13_par AND t1.hole13>t2.hole13_par-2
     OR t1.hole14<t2.hole14_par AND t1.hole14>t2.hole14_par-2
     OR t1.hole15<t2.hole15_par AND t1.hole15>t2.hole15_par-2
     OR t1.hole16<t2.hole16_par AND t1.hole16>t2.hole16_par-2
     OR t1.hole17<t2.hole17_par AND t1.hole17>t2.hole17_par-2
     OR t1.hole18<t2.hole18_par AND t1.hole18>t2.hole18_par-2
GROUP BY t1.player_id ORDER BY count(t1.player_id) DESC";
$result_p321 = mysql_query($query_p321);
$number = 1;
while ($row_p321 = mysql_fetch_array($result_p321)) {
    $player_id2 = $row_p321["player_id"];
    }
and so on..

You'll notice the "-2" in there. That is taking the par minus 2, as I don't want to record if the person is 2 strokes under. Just one stroke under. Any help is appreciated. Thank you. Oh, also, GROUP BY needs to be used as I don't want to list the player name more than once. Just want it to count all the birdies. I guess my big problem is its not counting more than 1 per row. Thanks.


Try something like that:

SELECT t1.*, SUM( IF(t1.hole1 = t2.hole1_par-1,1,0) +
        IF(t1.hole2 = t2.hole2_par-1,1,0) +
        IF(t1.hole3 = t2.hole3_par-1,1,0) +
        IF(t1.hole4 = t2.hole4_par-1,1,0) +
       -- etc.
        IF(t1.hole18 = t2.hole18_par-1,1,0) ) AS birdies
FROM scorecards t1
LEFT JOIN courses t2 ON t1.course_id=t2.course_id
GROUP BY t1.player_id
ORDER BY birdies DESC