MySQL Wildcards

In MySQL, wildcards are special characters that are used with the LIKE operator to perform pattern matching on a string column. The following are the commonly used wildcards in MySQL:

%' Wildcard

The % wildcard matches any string of zero or more characters. For example, to find all rows where the last_name column starts with the letter “S”, you would use the following query:

SELECT * FROM employees WHERE last_name LIKE 'S%';

This would return all rows where the last_name column starts with the letter “S”.

_‘ Wildcard

The _ wildcard matches any single character. For example, to find all rows where the first_name column has exactly four characters, you would use the following query:

SELECT * FROM employees WHERE first_name LIKE '____';

This would return all rows where the first_name column has exactly four characters.

[]‘ Wildcard

The [] wildcard matches any single character from a list of characters. For example, to find all rows where the last_name column starts with the letter “S” or “T”, you would use the following query:

SELECT * FROM employees WHERE last_name LIKE '[ST]%';

This would return all rows where the last_name column starts with the letter “S” or “T”.

[^]‘ Wildcard

The [^] wildcard matches any single character not in a list of characters. For example, to find all rows where the last_name column does not start with the letter “S” or “T”, you would use the following query:

SELECT * FROM employees WHERE last_name LIKE '[^ST]%';

This would return all rows where the last_name column does not start with the letter “S” or “T”.

You can also use combinations of these wildcards to perform more complex pattern matching on string columns.