
- 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
Count items in a MySQL table with type ENUM involved?
You can use GROUP BY along with aggregate function COUNT(). Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Size ENUM('S','M','L','XL') ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Size) values('L'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(Size) values('S'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Size) values('S'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable(Size) values('M'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Size) values('XL'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Size) values('M'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Size) values('M'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Size) values('M'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Size) values('XL'); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+------+ | Id | Size | +----+------+ | 1 | L | | 2 | S | | 3 | S | | 4 | M | | 5 | XL | | 6 | M | | 7 | M | | 8 | M | | 9 | XL | +----+------+ 9 rows in set (0.00 sec)
Following is the query to count items in table with table filed as ENUM −
mysql> SELECT tbl.Size, COUNT(1) AS Frequency from DemoTable tbl group by tbl.Size;
This will produce the following output −
+------+-----------+ | Size | Frequency | +------+-----------+ | L | 1 | | S | 2 | | M | 4 | | XL | 2 | +------+-----------+ 4 rows in set (0.00 sec)
- Related Articles
- What is MySQL ENUM data type? What are the advantages to use ENUM data type?
- Count the number of columns in a MySQL table with Java
- Enum with NOT NULL in a MySQL field?
- Do MySQL Can Enum type values contain spaces in it?
- Add a new value to a column of data type enum in MySQL?
- How can I insert default value in MySQL ENUM data type?
- Adding new enum column to an existing MySQL table?
- Count duplicates records in MySQL table?
- How to enumerate an enum with String type?
- what are the different attributes of MySQL ENUM data type?
- Count rows having three or more rows with a certain value in a MySQL table
- How to count items in array with MongoDB?
- Select records with ACTIVE status in MySQL set with ENUM
- How do we use an enum type with a constructor in Java?\n
- MySQL ORDER BY 'ENUM' type value based on conditions

Advertisements