0

我想查找我的表中是否有任何重复的票号。我下面的代码显示了类表中的第一个票号,即 1039,如果有重复,我想显示。1039 不是重复的。我的问题是什么?

$query = "SELECT ticket, COUNT(ticket) AS NumOccurrences FROM classes GROUP BY ticket HAVING (COUNT(ticket) > 1)";
$result = mysql_query($query) or die(mysql_error());    
while($row = mysql_fetch_array($result))    
{
$ticket = $row['ticket'];
}

if($result)
{
echo $ticket. "<br/>";
echo "there are " . mysql_num_rows($result) . " tickets ";
}

else
{
echo "no duplicate ";
}
4

2 回答 2

1

您需要一个GROUP BY条款,否则您将计算所有门票。

SELECT ticket, COUNT(*) c FROM classes GROUP BY ticket HAVING (c > 1)
于 2012-08-10T20:02:22.303 回答
-1

尝试

SELECT ticket, COUNT(ticket) as `count` FROM classes GROUP BY ticket WHERE `count` > 1
于 2012-08-10T20:02:42.617 回答