在 PHP / Mysql 中打印重复记录

发布于 2024-11-19 04:20:27 字数 284 浏览 1 评论 0原文

我正在尝试打印表的重复记录,但只有单行得到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

诺曦 2024-11-26 04:20:27
$q = mysql_query("SELECT * FROM add WHERE cust_id = '144' GROUP BY cust_id");

while($r = mysql_fetch_array($q))
{
    print_r($r);
}
$q = mysql_query("SELECT * FROM add WHERE cust_id = '144' GROUP BY cust_id");

while($r = mysql_fetch_array($q))
{
    print_r($r);
}
緦唸λ蓇 2024-11-26 04:20:27

您需要循环遍历整个记录集...您只抓取第一行:

$resultset = mysql_query("select * from add where cust_id = '144' group by cust_id");
while($row = mysql_fetch_assoc($resultset))
{
   echo $row['column_name'];
}

You need to loop through the entire record set... you are only grabbing the first row:

$resultset = mysql_query("select * from add where cust_id = '144' group by cust_id");
while($row = mysql_fetch_assoc($resultset))
{
   echo $row['column_name'];
}
最笨的告白 2024-11-26 04:20:27

实际上,由于 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?

知足的幸福 2024-11-26 04:20:27

好吧,如果你想获取重复的值,那么这个查询会很好地为你服务:

T = the table
f = 检查重复项的字段
id = 行 id

select id,f from T group by f having count(f)= 2;

或 >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

select id,f from T group by f having count(f)= 2;

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.

幻想少年梦 2024-11-26 04:20:27

请尝试以下操作:

$q = mysql_query("SELECT * FROM add WHERE cust_id = '144'");
while($r = mysql_fetch_array($q))
{
    echo $r;
}

Try the following:

$q = mysql_query("SELECT * FROM add WHERE cust_id = '144'");
while($r = mysql_fetch_array($q))
{
    echo $r;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文