我有桌子我从显示sql记录得到它和这个表我用php代码,像这样的表
pos. name points
1 john 8
2 mike 5
3 mike 9
4 john 2
此表的php代码显示来自phpmyadmin sql的数据库中的记录
<?php
// make connecion
mysql_connect('localhost', 'db user', 'db password');
// Select Database
mysql_select_db ('db name');
$sql="SELECT * FROM wp_wp_pro_quiz_toplist";
$records=mysql_query($sql);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>الاحصائيات النهائية لمسابقة اكتوبر</title>
</head>
<body>
<table class="wpProQuiz_toplistTable">
<tr>
<th style="width: 40px;">Pos.</th>
<th style="">Name</th>
<th style="width: 60px;">Points</th>
<tr>
<?php
while($wp_wp_pro_quiz_toplist=mysql_fetch_assoc($records)) {
echo "<tr>";
echo "<td>".$wp_wp_pro_quiz_toplist[toplist_id]."</td>";
echo "<td>".$wp_wp_pro_quiz_toplist[name]."</td>";
echo "<td>".$wp_wp_pro_quiz_toplist[points]."</td>";
echo "</tr>";
}// End While
?>
</table>
</body>
</html>
正如你在表格中看到的那样,我有两次重复的名字,比如'mike'节目2次。
我希望根据每个名称的多个点的收据,对表格中的每个名称和数组或订单名称进行求和,如上所述
pos. name points
1 mike 14
2 john 10
请编辑我上面的PHP代码并输入你的答案,因为我是新手,不能使用完美的代码,我需要你的帮助很快,抱歉小英语
答案 0 :(得分:1)
试试这个......
$sql="SELECT sum(points) as sumpoints , name FROM wp_wp_pro_quiz_toplist group by name";
$records=mysql_query($sql);
答案 1 :(得分:0)
您应该更改查询以执行此操作:
mysql> select pos, name, SUM(points) as total from wp_wp_pro_quiz_toplist group by name;
+------+------+--------+
| pos | name | total |
+------+------+--------+
| 1 | john | 10 |
| 2 | mike | 14 |
+------+------+--------+
2 rows in set (0.00 sec)
如果您希望按总计点排序,那么您应该发出此查询(考虑到 pos 值是错误的):
mysql> select pos, name, SUM(points) as total from wp_wp_pro_quiz_toplist group by name order by total DESC;
+------+------+-------+
| pos | name | total |
+------+------+-------+
| 2 | mike | 14 |
| 1 | john | 10 |
+------+------+-------+
2 rows in set (0.00 sec)