结果按列1组合行然后按列2将SUM合并为DESC的总THEN顺序

时间:2013-01-16 23:54:28

标签: mysql sum multiple-columns

我有一个包含4列的数据库:

  1. 位置
  2. 日期
  3. TEAM_NAME
  4. 得分

我需要结果页面选择并组合“Team_Name”WHEN等于和SUM“得分”AS总计,并且仅在单个位置“BW3Avon”显示当前月份。

这是我的代码

<?php
$con = mysql_connect("localhost","*******_admin","g********1");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("x0kqgoub_bw3avonscore", $con);

$sql = "SELECT * FROM bw3avonscore WHERE MONTH(myDate) = MONTH(CURRENT_DATE)";

$result = mysql_query("SELECT * FROM bw3avonscore Team_Name ORDER BY Score DESC");

while($row = mysql_fetch_array($result))
{
echo $row['Team_Name'] . " " . $row['Score'];
echo "<br />";
}
mysql_close($con);
?>

1 个答案:

答案 0 :(得分:0)

SELECT  Team_Name, SUM(Score) totalScore
FROM    tableName
WHERE   Location = 'BW3Avon' AND
        MONTH(Date) = MONTH(CURDATE())
GROUP   BY Team_Name