2

我正在为和我一起打球的人制作一个关于高尔夫的统计页面。我试图从数据库中提取我们收到小鸟的所有记分卡的次数(低于标准杆 -1)。它确实在每个洞拉出了-1,但是我注意到如果你在记分卡上有 2 只小鸟,它仍然只算作 1 只小鸟而不是 2 只。我希望它继续计数,所以如果有人得到 9 只小鸟,那些总共增加了 9 个。

$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..

你会注意到那里的“-2”。那是标准杆负 2,因为我不想记录这个人是否低于 2 杆。只差一杆。任何帮助表示赞赏。谢谢你。哦,另外,需要使用 GROUP BY,因为我不想多次列出玩家姓名。只是想让它计算所有的小鸟。我想我的大问题是每行不超过 1 个。谢谢。

4

2 回答 2

1

问题是where条款。您需要在select子句中进行比较才能计算它们:

SELECT t1.*,
       sum((t1.hole1 = t2.hole1_par - 1) +
           (t1.hole2 = t2.hole2_par - 1) +
           . . .
           (t1.hole18 = t2.hole18_par - 1)
          ) 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

这使用了 MySQL 约定,即 true 为 1,false 0 将数字相加。使用标准 SQL 的另一种表述是:

       sum((case when t1.hole1 = t2.hole1_par - 1) then 1 else 0 end) +
于 2013-03-29T21:44:20.697 回答
0

尝试这样的事情:

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
于 2013-03-29T21:42:48.740 回答