简体   繁体   中英

How to count the distinct values repeated in table

I have a table which has columns as id | BSC | Size .Now each column has some data in it like

       id |   BSC     |  Size

       1    Tambaram     100
       2    Tambaram     200
       3    Perungudi    100
       4    Tambaram     100
       5    Perungudi    200

I want to find the count of the number of distinct values and how many times each of those distinct values are repeated like 100 is repeated 3 times and 200 for 2 times. I have used COUNT and DISTINCT and getting the count of the number of distinct values. But i also want to know how many times those are repeated. Here is my code:

<?php
include 'db.php';
if (!$link)
  {
  die('Could not connect: ' . mysqli_connect_errno());
  }
$result = mysqli_query($link,"SELECT COUNT(DISTINCT Size) FROM Query_count");
if($result){
    $data = mysqli_fetch_array($result);
    echo $data[0];
}
$link->close();
?>

You are missing a group by like

SELECT COUNT(DISTINCT Size) as sizes
FROM Query_count
GROUP BY BSC

If you want to know how many time single SIZE values are present you shuold use

SELECT SIZE, COUNT(*) AS RN
FROM QUERY_COUNT
GROUP BY SIZE

You are missing group by size:

SELECT size, count(*) as count from Query_count
GROUP BY size    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM