
- 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
Distinct number of specific items in list with MySQL
To find distinct number of specific items, use COUNT() along with GROUP BY clause. Let us first create a table −
mysql> create table DemoTable1854 ( Name varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1854 values('John-Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1854 values('Chris-Brown'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1854 values('Adam-Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1854 values('John-Doe'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1854 values('John-Smith'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1854;
This will produce the following output −
+-------------+ | Name | +-------------+ | John-Smith | | Chris-Brown | | Adam-Smith | | John-Doe | | John-Smith | +-------------+ 5 rows in set (0.00 sec)
Here is the query to get the distinct number of specific items in list −
mysql> select Name,count(Name) from DemoTable1854 where Name like 'John-%' group by Name;
This will produce the following output −
+------------+-------------+ | Name | count(Name) | +------------+-------------+ | John-Smith | 2 | | John-Doe | 1 | +------------+-------------+ 2 rows in set (0.00 sec)
- Related Articles
- Select random number from a specific list in MySQL?
- Counting different distinct items in a single MySQL query?
- Count the number of distinct values in MySQL?
- MongoDB query to get a specific number of items
- Program to find maximum number of K-sized groups with distinct type items are possible in Python
- Display duplicate record as a distinct value with corresponding values as distinct comma separated list in MySQL?
- MySQL select * with distinct id?
- Sort items in MySQL with dots?
- How to find a specific record from a list of values with semicolon in MySQL?
- MySQL RegExp to fetch records with only a specific number of words
- How to count the number of items in a C# list?
- Swapping items of a list in Java _ Collections.swap() with Example
- Finding number of occurrences of a specific string in MySQL?
- MySQL REGEXP to fetch string + number records beginning with specific numbers?
- Implementing incremental search and display the values with a specific number in MySQL?

Advertisements