我想计算重复项并将其输出为数字。
数据库:
user
Sam
Sam
Tom
Tom
Tom
John
这是我的代码:
$sql = "select user, count(*) duplicates from users group by user order by duplicates desc";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["user"]. "<br>";
}
} else {
}
这行得通,但这是输出:
Tom
Sam
John
我希望输出仅是数字:
3
2
1
感谢您的回答。
答案 0 :(得分:1)
您可以使用SQL
进行计数(就像以前一样)并将数字存储在属性中。可以在PHP中直接读取此属性。您必须在SQL中使用AS
声明计数变量。
$sql = "select user, count(*) AS duplicates from users group by user order by duplicates desc";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo $row["duplicates"]. "<br>";
}
}
else
{
}
答案 1 :(得分:0)
选择[mysqld]
#Path to installation directory. All paths are usually resolved relative to this.
basedir = "C:/Program Files (x86)/EasyPHP-Devserver-17/eds-binaries/dbserver/mysql5717x86x191205223118/"
skip-grant-tables <=== added this line
列,而不是选择user
列:
ID
然后在您的$sql = "select ID, count(*) duplicates from users group by user order by duplicates desc";
中输出ID列:
echo