MySQL - Use of AND & OR in the same query

13

I have a table like this one, which has two rows:

nombre_per   | apellido_per   | permisos_busqueda_per
-------------+----------------+----------------------
Mattew       | Janeey         | SI
Janny        | Pineda         | NO

My query is as follows

SELECT nombre_per, apellido_per, permisos_busqueda_per 
FROM   la_tabla 
WHERE  nombre_per LIKE '%a%' OR apellido_per LIKE '%a%' AND permisos_busqueda_per="SI"

If I'm only looking for users who have permission to be searched ( permisos_busqueda_per ), why do I get the two rows if Janny does not have permission to be found?

    
asked by Máxima Alekz 17.06.2016 в 13:55
source

6 answers

18

This is a problem with the precedence of the operators, as you can see in the documentation of MySQL , the AND operator takes precedence over the OR, this makes your SELECT select based on these conditions:

  • people who have a name with the letter "a"; O
  • people with a surname with the letter "a" and permits equal to "SI"

when really what you want is this:

  • people who have a name or surname with the letter "a"; And
  • permits equal to "YES"

The solution is simple: use parentheses:

SELECT nombre_per, apellido_per, permisos_busqueda_per 
FROM   la_tabla 
WHERE  (nombre_per LIKE '%a%' OR apellido_per LIKE '%a%') AND permisos_busqueda_per="SI"
    
answered by 17.06.2016 / 14:15
source
5

Try grouping the conditions with parentheses

SELECT nombre_per, apellido_per, permisos_busqueda_per FROM la_tabla WHERE (nombre_per LIKE '%a%' OR apellido_per LIKE '%a%') AND permisos_busqueda_per="SI
    
answered by 17.06.2016 в 14:08
3

You have an error in the query, you have to change:

SELECT * FROM nombre_per, apellido_per, permisos_busqueda_per FROM la_tabla WHERE nombre_per LIKE '%a%' OR apellido_per LIKE '%a%' AND permisos_busqueda_per="SI"

Because of this:

    SELECT nombre_per, apellido_per, permisos_busqueda_per FROM la_tabla WHERE nombre_per LIKE '%a%' AND permisos_busqueda_per= 'SI'
 OR apellido_per LIKE '%a%' AND permisos_busqueda_per= 'SI'

The problem was in the 'or'. I hope it serves you

    
answered by 17.06.2016 в 14:03
3

How they have commented on the other answers, your problem is that the operators according to the documentation do not follow the process you expect; I recommend that when you use operations, do it like this:

SELECT nombre_per, apellido_per, permisos_busqueda_per 
FROM   la_tabla 
WHERE  (nombre_per LIKE '%a%' OR apellido_per LIKE '%a%') 
        AND permisos_busqueda_per="SI"

Separating the AND in different lines and always using () in each line.

    
answered by 17.06.2016 в 14:18
1

That's it, besides I'm not sure about mysql, but in sql you have to use single quotes for the strings, that is 'instead of'

    
answered by 17.06.2016 в 15:56
1

other people have already documented it, but what you have to know is that the order of the factors influences a lot in the consultations, it brings you the two values because the last thing that the last thing you are doing is the OR, that's why that you must put it inside the parentheses to differentiate between one operation and another and the manager can not be confused.

SELECT per_name, per_per_name, permissions_search_per FROM the_table WHERE (name_per LIKE '% a%' OR surname_per LIKE '% a%')         AND permissions_busqueda_per="SI"

You must remember that the order of execution in this case after the Where goes first the parentheses and then the AND, you have already selected all your first and last name has an "a" and then proceed to verify the permissions

I hope you find the information helpful

    
answered by 21.06.2016 в 07:34