What's the difference between LIKE, IN and BETWEEN in MySQL?

11

I'm doubtful about the difference and use of the LIKE , IN and BETWEEN tags in MySQL. When should I use the tags and example?

    
asked by anonymous 13.11.2017 / 15:27

4 answers

9

LIKE clause / seller

The% / LIKE clause is used when you want to search for a character, using coringa or % , if you want to scan characters that begin with the word A the correct one is to do as I will show below. First notice the position of coringa % because it makes a difference.

Here you will find all the records that contain the letter A in the word. In the first alternative only with the wildcard % and the second %_caracter_%

SELECT * FROM tabela WHERE descricao LIKE "%A%" 
SELECT * FROM tabela WHERE descricao LIKE "%_A_%"    

Here you will find all the records that contain the letter A at the end of the word. In the first alternative only with the wildcard % and the second %_caracter

SELECT * FROM tabela WHERE descricao LIKE "%A"
SELECT * FROM tabela WHERE descricao LIKE "%_A"

Here you will find all the records that contain the letter A at the beginning of the word. In the first alternative only with the wildcard % and the second caracter_%

SELECT * FROM tabela WHERE descricao LIKE "A%"
SELECT * FROM tabela WHERE descricao LIKE "A_%" 

Now let's go to the BETWEEN clause

You should use the BETWEEN clause when you want to take only a range of your SELECT, meaning you want to know the sales made between 10/11/2017 and 11/13/2017. After all the translation of BETWEEN would be Entre . I will indicate in the select below that I want to fetch the records between 10/11/2017 and 11/13/2017. Remembering that to search for an interval one must indicate only 2 conditions

Select  * FROM tabela WHERE vendas BETWEEN 10/11/2017 AND 13/11/2017

Now let's clause IN

The IN clause is used when we want to query a table by filtering the value of one of its fields, from a list of possibilities. That is, if you want to give SELECT to a table where you have N records but want to filter them by searching only what you think is necessary. See, below I will indicate that I want to get the records of people who have ID = 1,2,5,10 only.

SELECT * from pessoas WHERE id IN (1,2,5,10)
    
13.11.2017 / 15:40
5

Examples in the rray response:

LIKE : is used to do partial searches in text type fields (varhcar text etc) using wildcards% _.

SELECT * FROM tabela
WHERE campo LIKE 'abc'

SELECT * FROM tabela
WHERE campo LIKE '%abc%'

IN : Compares a value against a fixed set or even with a subquery.

SELECT * FROM tabela
WHERE campo IN ('abc','def')

SELECT * FROM tabela
WHERE campo IN (10,20,30)

SELECT * FROM tabela
WHERE campo IN (SELECT campo FROM tabela
    WHERE campo LIKE '%abc%')

BETWEEN : Compares a value between a range of exactly two values.

SELECT * FROM tabela
WHERE campo BETWEEN '01/01/2017' AND '01/10/2017'

SELECT * FROM tabela
WHERE campo BETWEEN 10 AND 20
    
13.11.2017 / 15:38
2
  • LIKE : is used to do partial searches in text type fields (varhcar text etc) using wildcards % and _ .

  • IN : Compares a value against a fixed set or even with a subquery.

  • BETWEEN : Compares a value between a range of exactly two values.

Related:

What's the difference between SOME, IN and ANY?

Not IN or Not EXISTS to use?

What is the difference between the '=' and 'LIKE' operators?

    
13.11.2017 / 15:34
1

Between:

Definition , used to return records based in a range of values. Proof for fields of numeric type or date, eg:

SELECT * FROM Funcionario WHERE Admissao BETWEEN '2017-09-01' AND '2017-10-01'

SQLFiddle - See the example working

IN and Not IN:

Definition , Used to return records whose values meet or do not meet a particular list.

SELECT * FROM Funcionario WHERE Admissao IN ('2017-11-12', '2017-09-12')

SELECT * FROM Funcionario WHERE Admissao NOT IN ('2017-11-12', '2017-09-12')

SQLFiddle - View the example with IN working | SQLFiddle - example with NOT IN working

Like and Not Like:

Definition , used to return records that whether or not they contain the reported character string. Uses % to control the search form. You can vary your search in 3 ways:

SQLFiddle - Values started with a given character set.

SELECT * FROM Funcionario WHERE Pessoa like 'Cai%'

SQLFiddle - Values that end with a given character set.

SELECT * FROM Funcionario WHERE Pessoa like '%los'

SQLFiddle - Values that contain a certain character set.

SELECT * FROM Funcionario WHERE Pessoa like '%ai%'
    
13.11.2017 / 16:01