MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator:
%
represents zero, one, or multiple characters_
represents a single characterExample 1: Using %
Wildcard
Consider a table named employees
:
id | name |
---|---|
1 | John Doe |
2 | Jane Doe |
3 | Alice |
4 | Bob |
5 | Charlie |
6 | Doe Rayme |
Let's say you want to find any names that start with 'J'. You would use the %
wildcard at the end of your pattern, like this:
SELECT * FROM employees WHERE name LIKE 'J%';
This would return:
id | name |
---|---|
1 | John Doe |
2 | Jane Doe |
Example 2: Using _
Wildcard
Now, let's say you want to find any names that have 'o' as the second character. You would use the _
wildcard at the start of your pattern, and then the character you're interested in:
SELECT * FROM employees WHERE name LIKE '_o%';
This would return:
id | name |
---|---|
1 | John Doe |
2 | Jane Doe |
6 | Doe Rayme |
Example 3: Using both %
and _
Wildcards
You can also combine both wildcards in a pattern. For example, let's say you want to find any names that have 'o' as the second character and end with 'e'. You would use the _
wildcard at the start of your pattern, %
in the middle, and the character you're interested in at the end:
SELECT * FROM employees WHERE name LIKE '_o%e';
This would return:
id | name |
---|---|
6 | Doe Rayme |
Remember, LIKE
is case insensitive. If you need case sensitive matching, consider using LIKE BINARY
.
Keep in mind, while the LIKE operator can be very useful for pattern matching, it can lead to performance issues if not used carefully, especially on large tables, as it may not be able to use indexes effectively.
MySQL LIKE Operator Example:
SELECT * FROM table_name WHERE column_name LIKE 'pattern';
How to Use LIKE in MySQL for Fuzzy Queries:
SELECT * FROM table_name WHERE column_name LIKE '%pattern%';
Fuzzy Matching with LIKE in MySQL:
SELECT * FROM table_name WHERE column_name LIKE 'part%';
Wildcard Characters in MySQL LIKE:
SELECT * FROM table_name WHERE column_name LIKE 'a%';
Pattern Matching Using MySQL LIKE:
SELECT * FROM table_name WHERE column_name LIKE 'abc_';
Case-sensitive vs Case-insensitive LIKE in MySQL:
SELECT * FROM table_name WHERE column_name LIKE 'pattern' COLLATE Latin1_General_CS;
Optimizing Fuzzy Queries with MySQL LIKE:
CREATE INDEX index_name ON table_name (column_name);
Using Multiple Wildcards in MySQL LIKE:
SELECT * FROM table_name WHERE column_name LIKE 'a%b_';
Examples of Fuzzy Queries with LIKE in MySQL:
SELECT * FROM products WHERE product_name LIKE '%apple%';