
- 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 case-insensitive DISTINCT?
If you want case-insensitive distinct, you need to use UPPER() or LOWER().
Case 1: Using UPPER().
The syntax is as follows:
SELECT DISTINCT UPPER(yourColumnName) FROM yourTableName;
Case 2: Using LOWER().
The syntax is as follows:
SELECT DISTINCT LOWER(yourColumnName) FROM yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table CaseInsensitiveDistinctDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> UserEmailId varchar(30), -> UserPassword varchar(10), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.64 sec)
Now you can insert some records in the table using insert command. The query is as follows:
mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('John@gmail.com','john123'); Query OK, 1 row affected (0.15 sec) mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('john@gmail.com','654321'); Query OK, 1 row affected (0.43 sec) mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('Mike@gmail.com','999999'); Query OK, 1 row affected (0.14 sec) mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('mike@gmail.com','334556'); Query OK, 1 row affected (0.16 sec) mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('Carol@gmail.com','1010101'); Query OK, 1 row affected (0.13 sec) mysql> insert into CaseInsensitiveDistinctDemo(UserEmailId,UserPassword) values('Larry@gmail.com','12345678'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement. The query is as follows:
mysql> select *from CaseInsensitiveDistinctDemo;
The following is the output:
+----+-----------------+--------------+ | Id | UserEmailId | UserPassword | +----+-----------------+--------------+ | 1 | John@gmail.com | john123 | | 2 | john@gmail.com | 654321 | | 3 | Mike@gmail.com | 999999 | | 4 | mike@gmail.com | 334556 | | 5 | Carol@gmail.com | 1010101 | | 6 | Larry@gmail.com | 12345678 | +----+-----------------+--------------+ 6 rows in set (0.00 sec)
Here is the query to select case-insensitive distinct.
Case 1: Using UPPER(). The query is as follows:
mysql> select distinct upper(UserEmailId) from CaseInsensitiveDistinctDemo;
The following is the output:
+--------------------+ | upper(UserEmailId) | +--------------------+ | JOHN@GMAIL.COM | | MIKE@GMAIL.COM | | CAROL@GMAIL.COM | | LARRY@GMAIL.COM | +--------------------+ 4 rows in set (0.06 sec)
Case 2: Using LOWER(). The query is as follows:
mysql> select distinct lower(UserEmailId) from CaseInsensitiveDistinctDemo;
The following is the output:
+--------------------+ | lower(UserEmailId) | +--------------------+ | john@gmail.com | | mike@gmail.com | | carol@gmail.com | | larry@gmail.com | +--------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Perform case insensitive SELECT using MySQL IN()?
- How to achieve case sensitive uniqueness and case insensitive search in MySQL?
- Is Python case-sensitive or case-insensitive?
- Case-insensitive Dictionary in C#
- Case insensitive search in Mongo?
- JavaScript Count characters case insensitive
- How to use case-insensitive switch-case in JavaScript?
- Case-insensitive string comparison in C++
- JavaScript Check for case insensitive values?
- MongoDB query with case insensitive search?
- MongoDB query for specific case insensitive search
- Case-insensitive string replacement using Python Program
- How to make jQuery attribute selector case insensitive?
- How to perform case-insensitive search in Oracle?
- How to perform Case Insensitive matching with JavaScript RegExp?

Advertisements