LIKE
Sintassi
expr LIKE pat [ESCAPE 'escape_char'] expr NOT LIKE pat [ESCAPE 'escape_char']
Spiegazione
Verifica se expr corrisponde al pattern pat. Restituisce 1 (TRUE
) o 0 (FALSE
). Sia expr, sia pat possono essere una qualsiasi espressione valida, e vengono valutati come stringhe. I pattern possono utilizzare i seguenti caratteri jolly:
%
corrisponde a un qualsiasi numero di caratteri, anche zero._
corrisponde a un singolo carattere.
Si può utilizzare NOT LIKE
per verificare se una stringa non corrisponde a un pattern. Equivale a usare l'operatore NOT
sull'intera espressione LIKE
.
Se l'espressione o il pattern sono NULL
, il risultato è NULL
.
LIKE
cerca una corrispondenza case-sensitive (cioè tenendo conto della differenza tra le lettere minuscole e le maiuscole) se la collation dell'espressione è case-sensitive. Per effettuare ricerche case-insensitiva, si può dichiarare che uno degli argomenti ha una collation binaria, utilizzando COLLATE
, oppure convertirli in stringhe binarie
utilizzando CAST
. Si usi SHOW COLLATION
per ottenere la lista delle collation disponibili. Quelle che terminano con _bin
sono case-sensitive.
Gli argomenti numerici sono convertiti in stringhe binarie.
Il carattere jolly _
corrisponde a un singolo carattere, non a un byte. Può corrispondere a un carattere multi-byte solo se questo esiste nel set di caratteri dell'espressione. Per esempio, _
corrisponde a _utf8"€"
, ma non a _latin1"€"
perché il simbolo Euro non esiste nel set di caratteri latin1. Se necessario, si può usare CONVERT
per convertire l'espressione in un differente set di caratteri.
If you need to match the characters _
or %
, you must escape them. By default,
you can prefix the wildcard characters the backslash characer \
to escape them.
The backslash is used both to encode special characters like newlines when a string is
parsed as well as to escape wildcards in a pattern after parsing. Thus, to match an
actual backslash, you sometimes need to double-escape it as "\
\
\
\"
.
To avoid difficulties with the backslash character, you can change the wildcard escape
character using ESCAPE
in a LIKE
expression. The argument to ESCAPE
must be a single-character string.
Examples:
Select the days that begin with "T":
CREATE TABLE t1 (d VARCHAR(16)); INSERT INTO t1 VALUES ("Monday"), ("Tuesday"), ("Wednesday"), ("Thursday"), ("Friday"), ("Saturday"), ("Sunday"); SELECT * FROM t1 WHERE d LIKE "T%";
MariaDB [test]> SELECT * FROM t1 WHERE d LIKE "T%"; +----------+ | d | +----------+ | Tuesday | | Thursday | +----------+ 2 rows in set (0.00 sec)
Select the days that contain the substring "es":
SELECT * FROM t1 WHERE d LIKE "%es%";
MariaDB [test]> SELECT * FROM t1 WHERE d LIKE "%es%"; +-----------+ | d | +-----------+ | Tuesday | | Wednesday | +-----------+ 2 rows in set (0.00 sec)
Select the six-character day names:
SELECT * FROM t1 WHERE d like "___day";
MariaDB [test]> SELECT * FROM t1 WHERE d like "___day"; +---------+ | d | +---------+ | Monday | | Friday | | Sunday | +---------+ 3 rows in set (0.00 sec)
With the default collations, LIKE
is case-insensitive:
SELECT * FROM t1 where d like "t%";
MariaDB [test]> SELECT * FROM t1 where d like "t%"; +----------+ | d | +----------+ | Tuesday | | Thursday | +----------+ 2 rows in set (0.00 sec)
Use COLLATE
to specify a binary collation, forcing
case-sensitive matches:
SELECT * FROM t1 WHERE d like "t%" COLLATE latin1_bin;
MariaDB [test]> SELECT * FROM t1 WHERE d like "t%" COLLATE latin1_bin; Empty set (0.00 sec)
You can include functions and operators in the expression to match. Select dates based on their day name:
CREATE TABLE t2 (d DATETIME); INSERT INTO t2 VALUES ("2007-01-30 21:31:07"), ("1983-10-15 06:42:51"), ("2011-04-21 12:34:56"), ("2011-10-30 06:31:41"), ("2011-01-30 14:03:25"), ("2004-10-07 11:19:34"); SELECT * FROM t2 WHERE DAYNAME(d) LIKE "T%";
MariaDB [test]> SELECT * FROM t2 WHERE DAYNAME(d) LIKE "T%"; +------------------+ | d | +------------------+ | 2007-01-30 21:31 | | 2011-04-21 12:34 | | 2004-10-07 11:19 | +------------------+ 3 rows in set, 7 warnings (0.00 sec)