计算表的重复行并继续计算

时间:2019-01-03 00:09:27

标签: php mysqli

我在mysql数据库中有下表

database name = roulette

id | number | user

1 | 18 | John
2 | 14 | Administrator
3 | 14 | Administrator
4 | 19 | Administrator
5 | 17 | Administrator

现在我要在以下情况下继续:

系统必须仅考虑“管理员”用户的号码,如果至少有一个重复项,则继续计算。

所以在我的示例中,数字14至少重复了2次,因此我们必须继续进行操作。

我不知道如何从查询中获取此号码。我尝试了以下代码,但它不起作用:

$connectiondb = mysqli_connect($servername, $username, $password, $dbname);
$duplicates = mysqli_query($connectiondb, "SELECT number, COUNT(*) as count
FROM roulette
GROUP BY number
HAVING COUNT(*) > 0");

if(mysqli_num_rows($duplicates) > 0){
        echo "There is at least one duplicate number for the selected user. We proceed.";
}

加上此查询,我还会收到错误:

  

PHP可恢复的致命错误:mysqli_result类的对象无法   转换为字符串

2 个答案:

答案 0 :(得分:0)

尝试在查询中使用count(*)和以下命令来访问结果集:

while ($row = $result->fetch_assoc()) {
    echo $row['classtype']."<br>";
}

mysqli_query将对象资源返回给变量,而不是字符串。 参考:Object of class mysqli_result could not be converted to string in

答案 1 :(得分:0)

问题解决了。如果列中存在以下数字: 13 13 14 14 15 数字13和14至少重复两次。

$duplicate = mysqli_query($connectiondb, "SELECT id, COUNT(number) FROM roulette WHERE user = '$user' GROUP BY number HAVING COUNT(number) > 1");

echo  "Duplicate in this table: ".mysqli_num_rows($duplicate)."<br>";