
- 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
Group result in MySQL and show on list?
For this, use GROUP BY along with ORDER BY −
select yourColumnName,count(*) as anyAliasName from yourTableName group by yourColumnName order by yourColumnName;
Let us create a table −
mysql> create table demo7 −> ( −> id int NOT NULL AUTO_INCREMENT, −> first_name varchar(50) −> , −> primary key(id) −> ); Query OK, 0 rows affected (1.22 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo7(first_name) values('John'); Query OK, 1 row affected (0.09 sec) mysql> insert into demo7(first_name) values('David'); Query OK, 1 row affected (0.22 sec) mysql> insert into demo7(first_name) values('John'); Query OK, 1 row affected (0.07 sec) mysql> insert into demo7(first_name) values('Bob'); Query OK, 1 row affected (0.27 sec) mysql> insert into demo7(first_name) values('David'); Query OK, 1 row affected (0.11 sec) mysql> insert into demo7(first_name) values('David'); Query OK, 1 row affected (0.09 sec) mysql> insert into demo7(first_name) values('John'); Query OK, 1 row affected (0.26 sec) mysql> insert into demo7(first_name) values('John'); Query OK, 1 row affected (0.09 sec)
Display records from the table using select statement −
mysql> select *from demo7;
This will produce the following output −
+----+------------+ | id | first_name | +----+------------+ | 1 | John | | 2 | David | | 3 | John | | 4 | Bob | | 5 | David | | 6 | David | | 7 | John | | 8 | John | +----+------------+ 8 rows in set (0.00 sec)
Following is the query to group result in MySQL and show on list −
mysql> select first_name,count(*) as frequency from demo7 group by first_name order by first_name;
This will produce the following output −
+------------+-----------+ | first_name | frequency | +------------+-----------+ | Bob | 1 | | David | 3 | | John | 4 | +------------+-----------+ 3 rows in set (0.00 sec)
- Related Articles
- What is the alias to Show Tables in MySQL Result?
- Show a MySQL user-defined variables values in the result table?
- Using group by on two fields and count in MySQL?
- Using GROUP BY and MAX on multiple columns in MySQL?
- What would be the result on concatenating server_id and UUID in MySQL?
- MongoDB Aggregate group multiple result?
- Show constraints on table command in MySQL?
- Retrieving group by result with arrays in MongoDB?
- MySQL query to get result by month and year based on condition?
- Sort a list in MySQL and display a fixed result at the end of the column?
- Count values based on conditions and display the result in different columns with MySQL?
- Replacing numbers on a comma delimited result with MySQL?
- Group month and year in MySQL?
- Difference between SHOW INDEX, SHOW INDEXES and SHOW KEYS in MySQL?
- Manipulate Object to group based on Array Object List in JavaScript

Advertisements