MySQL COUNT(DISTINCT) function
COUNT(DISTINCT) function
MySQL COUNT(DISTINCT) function returns a count of number rows with different non-NULL expr values.
Syntax:
COUNT(DISTINCT expr,[expr...])
Where expr is a given expression.
MySQL Version: 5.6
Example: MySQL COUNT(DISTINCT) function
The following MySQL statement will count the unique 'pub_lang' and average of 'no_page' up to 2 decimal places for each group of 'cate_id'.
Sample table: book_mast
Code:
SELECT cate_id,COUNT(DISTINCT(pub_lang)), ROUND(AVG(no_page),2)
FROM book_mast
GROUP BY cate_id;
Sample Output:
mysql> SELECT cate_id,COUNT(DISTINCT(pub_lang)), ROUND(AVG(no_page),2) -> FROM book_mast -> GROUP BY cate_id; +---------+---------------------------+-----------------------+ | cate_id | COUNT(DISTINCT(pub_lang)) | ROUND(AVG(no_page),2) | +---------+---------------------------+-----------------------+ | CA001 | 2 | 264.33 | | CA002 | 1 | 433.33 | | CA003 | 2 | 256.67 | | CA004 | 3 | 246.67 | | CA005 | 3 | 245.75 | +---------+---------------------------+-----------------------+ 5 rows in set (0.00 sec)
PHP script
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>example-count-with-distinct- php mysql examples | w3resource</title>
<meta name="description" content="example-count-with-distinct- php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Counting the unique language of publishing and average number of pages up to two decimal places within each group of category id:</h2>
<table class='table table-bordered'>
<tr>
<th>Category id</th><th>Unique language of publishing</th><th>Average number of pages up to two decimal places</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT cate_id,COUNT(DISTINCT(pub_lang)),
ROUND(AVG(no_page),2)
FROM book_mast GROUP BY cate_id') as $row) {
echo "<tr>";
echo "<td>" . $row['cate_id'] . "</td>";
echo "<td>" . $row['COUNT(DISTINCT(pub_lang))'] . "</td>";
echo "<td>" . $row['ROUND(AVG(no_page),2)'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>
JSP script
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-count-with-distinct</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data = "SELECT cate_id,COUNT(DISTINCT(pub_lang)),ROUND(AVG(no_page),2)FROM book_mast GROUP BY cate_id";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Category id</td>
<td>Unique language of publishing</td>
<td>Average number of pages up to two decimal places</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("cate_id")%></TD>
<TD><%=rs.getString("COUNT(DISTINCT(pub_lang))")%></TD>
<TD><%=rs.getString("ROUND(AVG(no_page),2)")%></TD>
</TR>
<% } %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>
Online Practice Editor:
Previous:
COUNT() with group by
Next:
GROUP_CONCAT()
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises