LIKE

Description

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:

  • The percent sign % wildcard: means to match any sequence of characters (including empty character sequences).

  • %text: matches a string ending with “text”.

  • text%: matches a string starting with “text”.
  • %text%: Matches a string containing “text”.
  • Underscore _ wildcard: means match a single character.

  • te_t: can match “text”, “test”, etc.

  • Other characters: The LIKE operator is case-sensitive for other characters.

Syntax

1. > SELECT column1, column2, ...
2. FROM table_name
3. WHERE columnN LIKE pattern;

Examples

1. drop table t1;
2. create table t1(a varchar(20));
3. insert into t1 values ('abc'), ('ABC'), ('abC');
4. select * from t1 where a ilike '%abC%';

6. mysql> select * from t1 where a like '%abC%';
7. +------+
8. | a    |
9. +------+
10. | abC  |
11. +------+
12. 1 row in set (0.00 sec)