
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to count occurrences of distinct values and display the result in a new column?
Let us first create a table −
mysql> create table DemoTable ( Value int ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 80 | | 90 | | 90 | | 80 | | 100| | 90 | +-------+ 6 rows in set (0.00 sec)
Following is the query to count occurrences of distinct values −
mysql> select Value,count(*) from DemoTable group by Value;
This will produce the following output −
+-------+----------+ | Value | count(*) | +-------+----------+ | 80 | 2 | | 90 | 3 | | 100 | 1 | +-------+----------+ 3 rows in set (0.00 sec)
- Related Articles
- Count number of occurrences of records in a MySQL table and display the result in a new column?
- MySQL query to count the duplicate ID values and display the result in a separate column
- Query to divide the values of two columns and display the result in a new column using MySQL wildcard?
- MySQL query to get the count of values and display the count in a new column ordered in descending order\n
- Count occurrences of known distinct values in MySQL
- How to round MySQL column with float values and display the result in a new column?
- MySQL query to group by names and display the count in a new column
- MySQL query to get the length of all columns and display the result in a single new column?
- MySQL query to display the count of distinct records from a column with duplicate records
- MySQL query to get the count of distinct records in a column
- MySQL query to display record with maximum count values in a group with other column values?
- “Structured” grouping query in MongoDB to display result with a new field displaying the count
- Count duplicate ids and display the result in a separate column with MySQL
- Display the count of duplicate records from a column in MySQL and order the result
- Setting column values as column names in the MySQL query result?

Advertisements