私のテーブル
ここで表示したいのは
各セクションで最高点を獲得した 2 人の学生の名前 (C1points から C3points の合計)。また、各競技とポイントを表示します。重複マークも表示する必要があります。
1 つのセクションについてのみお問い合わせください。
したがって、最終出力は次のようになります
これにより、単一のセクションの結果が得られます。
SELECT
Name,
SUM(C1Points) AS TotalC1,
SUM(C2Points) AS TotalC2,
SUM(C3Points) AS TotalC3,
SUM(C1Points + C2Points + C3Points) AS TotalAll
FROM myTable
WHERE Section = 'Section1'
GROUP BY Name
ORDER BY TotalAll DESC
LIMIT 2
MySQL ですべてのセクションを一度に読み取るのは複雑です。すべてを読んで正しく並べ替えてから、PHP を使用して各セクションの最初の 2 行を取得することをお勧めします。
SELECT
Name,
Section,
SUM(C1Points) AS TotalC1,
SUM(C2Points) AS TotalC2,
SUM(C3Points) AS TotalC3,
SUM(C1Points + C2Points + C3Points) AS TotalAll
FROM myTable
GROUP BY Name, Section
ORDER BY Section, TotalAll DESC
次のような結果セットが得られます。
Section 1 first place name and scores
Section 1 second place name and scores
Section 1 third place name and scores
. . .
Section 1 last place name and scores
Section 2 first place name and scores
Section 2 second place name and scores
and so on...
リストがこのように並べられている場合、各セクションの先頭 2 行を取得することは、PHP では簡単なことです。