1

i want to count and display each duplicate value in a column from the below table using php mysql

name  
AAA
BBB
BBB
BBB
AAA
CCC

result should be like below

AAA-2
BBB-3
CCC-1
3
  • can i give you sql query.? Commented Oct 13, 2017 at 4:52
  • you need sql query.? Commented Oct 13, 2017 at 4:53
  • Ccc is not duplicated Commented Oct 13, 2017 at 6:10

5 Answers 5

3

You need :

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

If you need output with hyphen (-)

select concat(name,'-',count(1)) from yourtable group by name
Sign up to request clarification or add additional context in comments.

Comments

2
select name, count(name) as count from table_name group by name

1 Comment

You can improve this answer by providing an explanation rather than just a bit of code.
1

try this,

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

Comments

1
SELECT name, COUNT(name) c_name FROM tablename GROUP BY name HAVING c_name>1 

1 Comment

Because of the HAVING clause, this does not provide the correct result.
-1

<?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>"; }

1 Comment

Am assumig you want to display data using php from the database. That will give you result: AAA-2 BBB-3 CCC-1 ( with your column name and table name respectively).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.