【问题标题】:count and display each duplicate value in a column using php mysql使用 php mysql 计算并显示列中的每个重复值
【发布时间】:2017-11-02 19:38:11
【问题描述】:

我想使用 php mysql 计算并显示下表中列中的每个重复值

name  
AAA
BBB
BBB
BBB
AAA
CCC

结果应该如下所示

AAA-2
BBB-3
CCC-1

【问题讨论】:

  • 可以给你sql查询吗?
  • 你需要sql查询吗?
  • 抄送不重复

标签: php mysql duplicates


【解决方案1】:
select name, count(name) as count from table_name group by name

【讨论】:

  • 您可以通过提供解释而不仅仅是一些代码来改进此答案。
【解决方案2】:

你需要:

select name,count(1) from yourtable group by name

如果您需要带连字符 (-) 的输出

select concat(name,'-',count(1)) from yourtable group by name

【讨论】:

    【解决方案3】:

    试试这个,

    select name, count(*) from <table> group by name;
    

    【讨论】:

      【解决方案4】:
      SELECT name, COUNT(name) c_name FROM tablename GROUP BY name HAVING c_name>1 
      

      【讨论】:

      • 由于 HAVING 子句,这不能提供正确的结果。
      【解决方案5】:

      <?php require_once('connection.php'); $result=mysqli_query($conn, "SELECT colum_name, concat(count(1),'-',colum_name) as colum_name FROM table_name group by colum_name order by colum_name desc"); while ( $data=mysqli_fetch_assoc($result)) { echo $data['colum_name']."<br>"; }

      【讨论】:

      • 我假设您想使用数据库中的 php 显示数据。这将为您提供结果:AAA-2 BBB-3 CCC-1(分别带有您的列名和表名)。
      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 2015-09-11
      • 2021-12-30
      • 2013-04-02
      • 1970-01-01
      相关资源
      最近更新 更多