
- 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
What are the different wildcard characters that can be used with MySQL LIKE operator?
As we know that LIKE operator is used along with WILDCARD characters to get the string having specified string. Basically, WILDCARD is the characters that help search data matching complex criteria. Followings are the types of wildcard which can be used in conjunction with LIKE operator
% -The Percentage
The ‘%’ wildcard is used to specify a pattern of 0, 1 or more characters. A basic syntax for using % wildcard is as follows
Select Statement…Where column_name LIKE ‘X%’
Here, X, is any specified starting pattern such as the single character of more and % matches any number of characters starting from 0.
The percentage ‘%’ wildcard can be in many ways with the specified pattern. The followings are some examples showing different LIKE operators with %. Here in these examples X represents the specified pattern.
1. LIKE ‘X%’:- It will find any values that start with “X”.
Example
mysql> Select * from Student Where name LIKE 'a%'; +------+-------+---------+---------+ | Id | Name | Address | Subject | +------+-------+---------+---------+ | 2 | Aarav | Mumbai | History | +------+-------+---------+---------+ 1 row in set (0.00 sec)
2. LIKE ‘%X’:- It will find any values that end with “X”.
Example
mysql> Select * from Student Where name LIKE '%v'; +------+--------+---------+-----------+ | Id | Name | Address | Subject | +------+--------+---------+-----------+ | 1 | Gaurav | Delhi | Computers | | 2 | Aarav | Mumbai | History | | 20 | Gaurav | Jaipur | Computers | +------+--------+---------+-----------+ 3 rows in set (0.00 sec)
3. LIKE ‘%X%’:- It will find any values have “X” in any position.
Example
mysql> Select * from Student Where name LIKE '%h%'; +------+---------+---------+----------+ | Id | Name | Address | Subject | +------+---------+---------+----------+ | 15 | Harshit | Delhi | Commerce | | 21 | Yashraj | NULL | Math | +------+---------+---------+----------+ 2 rows in set (0.00 sec)
4. LIKE ‘X%X’:-It will find any values that start with “X” and ends with “X”.
Example
mysql> Select * from Student Where name LIKE 'a%v'; +------+-------+---------+---------+ | Id | Name | Address | Subject | +------+-------+---------+---------+ | 2 | Aarav | Mumbai | History | +------+-------+---------+---------+ 1 row in set (0.00 sec)
_ The Underscore
The underscore wildcard is used to match exactly one character. A basic syntax for using _ wildcard is as follows −Select Statement…Where column_name LIKE ‘X_’
Here, X, is any specified starting pattern such as the single character of more and _ matches exactly one character.
The underscore ‘_’ wildcard can be used, alone or in combination with %, in many ways with the specified pattern. Followings are some examples showing different LIKE operators with %. Here in these examples X represents the specified pattern.
1. LIKE ‘X_’:- It will find any values that start with “X” and having exactly one character after X.
Example
mysql> Select * from student WHERE year_of_Admission LIKE '200_'; +------+---------+---------+-----------+--------------------+ | Id | Name | Address | Subject | year_of_Admission | +------+---------+---------+-----------+--------------------+ | 1 | Gaurav | Delhi | Computers | 2001 | | 15 | Harshit | Delhi | Commerce | 2009 | | 21 | Yashraj | NULL | Math | 2000 | +------+---------+---------+-----------+--------------------+ 3 rows in set (0.00 sec)
2. LIKE ‘_X’:- It will find any values that end with “X” and having exactly one character before X.
Example
mysql> Select * from student WHERE year_of_Admission LIKE '_017'; +------+--------+---------+-----------+--------------------+ | Id | Name | Address | Subject | year_of_Admission | +------+--------+---------+-----------+--------------------+ | 20 | Gaurav | Jaipur | Computers | 2017 | +------+--------+---------+-----------+--------------------+ 1 row in set (0.00 sec)
3. LIKE _X%:- It is in combination with % wildcard. It will find any values that have X in the second position.
Example
mysql> Select * from student WHERE Name LIKE '_a%'; +------+---------+---------+-----------+--------------------+ | Id | Name | Address | Subject | year_of_Admission | +------+---------+---------+-----------+--------------------+ | 1 | Gaurav | Delhi | Computers | 2001 | | 2 | Aarav | Mumbai | History | 2010 | | 15 | Harshit | Delhi | Commerce | 2009 | | 20 | Gaurav | Jaipur | Computers | 2017 | | 21 | Yashraj | NULL | Math | 2000 | +------+---------+---------+-----------+--------------------+ 5 rows in set (0.00 sec)
4. LIKE X_%_%:- It is in combination with % wildcard. It will find any values that start with X and at least three characters in length.
Example
mysql> Select * from student WHERE Name LIKE 'g_%_%'; +------+--------+---------+-----------+--------------------+ | Id | Name | Address | Subject | year_of_Admission | +------+--------+---------+-----------+--------------------+ | 1 | Gaurav | Delhi | Computers | 2001 | | 20 | Gaurav | Jaipur | Computers | 2017 | +------+--------+---------+-----------+--------------------+ 2 rows in set (0.00 sec)
- Related Articles
- What are the different wildcard characters that can be used with MySQL RLIKE operator?
- What are the different wildcard characters which can be used with NOT LIKE operator?
- How wildcard characters can be used with MySQL CONCAT() function?
- What are the different unit values that can be used with MySQL INTERVAL keyword?
- How to restrict MySQL `LIKE` operator to begin with specific characters?
- What are the different time format characters used by MySQL DATE_FORMAT() function?
- What are different date format characters used by MySQL DATE_FORMAT() function?
- What are the different commands used in MySQL?
- Display specific table names with MySQL LIKE Operator
- How Are MySQL INSTR() and LIKE operator similar?
- What is the use of MySQL NOT LIKE operator?
- What is the use of MySQL SOUNDS LIKE operator?
- What should be used to implement MySQL LIKE statement in MongoDB?
- What is the use of SOUNDS LIKE operator in MySQL?
- Why BINARY keyword used with MySQL REGEXP operator?
