在 PHP / Mysql 中打印重复记录
我正在尝试打印表的重复记录,但只有单行得到 echoed.然而在mysql中这个查询会产生所有重复的记录。这是查询:
$q = mysql_query("SELECT * FROM add WHERE cust_id = '144' GROUP BY cust_id");
$r = mysql_fetch_array($q);
$s = mysql_num_rows($q);
while($s !=0)
{
echo $r;
$s=$s-1;
}
代码有什么问题?
I am trying to print the duplicate records of the table but only the single row is getting
echoed.However in mysql this query results all the duplicate records. Here is the query:
$q = mysql_query("SELECT * FROM add WHERE cust_id = '144' GROUP BY cust_id");
$r = mysql_fetch_array($q);
$s = mysql_num_rows($q);
while($s !=0)
{
echo $r;
$s=$s-1;
}
Whats wrong with the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要循环遍历整个记录集...您只抓取第一行:
You need to loop through the entire record set... you are only grabbing the first row:
实际上,由于 GROUP BY 子句,您的 SQL 查询将仅返回 0 或 1 行。您绝对确定这是您在 mysql 中执行的查询吗?
Your SQL query will in practice only ever return 0 or 1 rows, due to the GROUP BY clause. Are you absolutely sure that that's the query you were executing in mysql?
好吧,如果你想获取重复的值,那么这个查询会很好地为你服务:
T = the table
f = 检查重复项的字段
id = 行 id
或 >2(如果您想要 f 中出现在多行中的每个值)。
having 与 where 类似,但在 group by 后进行评估。
well, if you want to get duplicate values, then this query will serve you well:
T = the table
f = the field to check for duplicates
id = the rows id
or >2 if you want every value in f that occurs in more than one row.
having is like where but evaluated after group by.
请尝试以下操作:
Try the following: