L'operatore LIKE
viene utilizzato con la clausola WHERE
per ottenere un set di risultati che corrisponda al modello di stringa specificata. Per esempio:
SELECT * FROM customers WHERE country LIKE 'UK';
LIKE
si comporta in modo simile all'operatore =
, anche se non sono la stessa cosa. L'operatore =
viene utilizzato per verificare l'uguaglianza mentre l'operatore LIKE
viene utilizzato solo per abbinare i modelli di stringa.
Possiamo invertire il funzionamento dell'operatore LIKE
ed ignorare il set di risultati che corrispondono allo schema di stringa specificato utilizzando l'operatore NOT
. Per esempio:
SELECT * FROM customers WHERE country NOT LIKE 'USA';
L'operatore LIKE
viene spesso utilizzato con i caratteri jolly per abbinare un modello di stringa. Un carattere jolly viene utilizzato per sostituire un singolo o un insieme di caratteri in qualsiasi stringa. Per esempio:
SELECT * FROM customers WHERE surname LIKE 'R%';
Il carattere jolly %
viene utilizzato per rappresentare zero o più caratteri. Per esempio:
SELECT * FROM customers WHERE surname LIKE 'R%';
stringa | matched? |
---|---|
R | match |
Run | match |
Mere | no match |
Summer | no match |
Il carattere jolly _
viene utilizzato per rappresentare esattamente un carattere in una stringa. Per esempio:
SELECT * FROM customers WHERE surname LIKE 'U_';
stringa | matched? |
---|---|
U | no match |
UK | match |
USA | no match |
Il carattere jolly []
viene utilizzato per rappresentare qualsiasi carattere tra parentesi. Per esempio:
SELECT * FROM customers WHERE country LIKE 'U[KA]%';
stringa | matched? |
---|---|
U | no match |
UK | match |
UAE | match |
USA | no match |
Il carattere jolly !
viene utilizzato per escludere i carattere da una stringa. Per esempio:
SELECT * FROM customers WHERE country LIKE '[!DR]%';
stringa | matched? |
---|---|
Doe | no match |
Reinhardt | no match |
Luna | match |
D | no match |
O | match |
R | no match |