
- 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
How to select all distinct filename extensions from a table of filenames in MySQL?
You can use DISTINCT along with SUBSTRING_INDEX() to extract the filename extensions. Let us first create a table−
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FileName text ); Query OK, 0 rows affected (0.75 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable(FileName) values('AddTwoValue.java'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(FileName) values('Image1.png'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(FileName) values('MultiplicationOfTwoNumbers.java'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(FileName) values('Palindrome.c'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(FileName) values('FoodCart.png'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(FileName) values('Permutation.py'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output−
+----+---------------------------------+ | Id | FileName | +----+---------------------------------+ | 1 | AddTwoValue.java | | 2 | Image1.png | | 3 | MultiplicationOfTwoNumbers.java | | 4 | Palindrome.c | | 5 | FoodCart.png | | 6 | Permutation.py | +----+---------------------------------+ 6 rows in set (0.00 sec)
Following is the query to select all distinct filename extensions from a table of filenames−
mysql> SELECT DISTINCT SUBSTRING_INDEX(FileName,'.',-1) FROM DemoTable;
This will produce the following output−
+----------------------------------+ | SUBSTRING_INDEX(FileName,'.',-1) | +----------------------------------+ | java | | png | | c | | py | +----------------------------------+ 4 rows in set (0.03 sec)
- Related Articles
- MySQL query to select average from distinct column of table?
- MySQL select distinct dates from datetime column in a table?
- Randomly SELECT distinct rows in a MySQL table?
- How to select all the data from a table using MySQL in Python?
- How to find all the distinct file extensions in a folder hierarchy (Linux)?
- How to select all rows from a table except the last one in MySQL?
- Extracting filenames from a path in MySQL?
- How to select distinct value from one MySQL column only?
- How can we write MySQL stored procedure to select all the data from a table?
- How to select all the records except a row with certain id from a MySQL table?
- Select distinct values from two columns in MySQL?
- Select distinct combinations from two columns in MySQL?
- MYSQL select DISTINCT values from two columns?
- How to speed up SELECT DISTINCT in MySQL
- How to select all the column names from a table in Laravel?

Advertisements