什么是对数据库中不止一次存在的记录进行计数的SQL查询?


What is an SQL query for counting records that are existing more than once in a database?

<?php
mysql_connect("localhost", "root", "");
mysql_select_db("students");
$id = $_POST['id'];
$grade = $_POST['grade'];
$query = "INSERT INTO `st_table` (`St_id`,`Grade`) VALUES ('$id','$grade')";
$result = mysql_query($query);
$query = "SELECT * from `st_table`";
$result = mysql_query($query);
echo "<table>"; 
echo "<th>St_id</th><th>Grade</th>";
while($row = mysql_fetch_array($result)){   
echo "<tr><td>" . $row['St_id'] . "</td><td>" . $row['Grade'] . "</td></tr>"; 
                           }

这段代码向表中添加了ID和Grade两个值。我想要另一个查询,将能够计算有多少a, b, c等,并输出它在html表上。

在这里,您的查询可以按Grade分组,而不是按Grades分组

"SELECT `Grade`, COUNT(*) AS count FROM `st_table` GROUP BY `Grade`";

这里是sqlfiddle

编辑后

我提到的查询应该为你工作,你可以检查小提琴,因为你修改的代码是关心你必须改变你的表一点,因为你要包括St_id,所以使它3列,并相应地改变查询。