我想查看表格中是否有重复的票号。 我下面的代码是显示类表中的第一个票号,即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 ";
}
答案 0 :(得分:1)
您需要GROUP BY
条款,否则您需要计算所有门票。
SELECT ticket, COUNT(*) c FROM classes GROUP BY ticket HAVING (c > 1)
答案 1 :(得分:-1)
尝试
SELECT ticket, COUNT(ticket) as `count` FROM classes GROUP BY ticket WHERE `count` > 1