Show data with filter in SQL query

1

Cordial greeting, I am trying to show the data of an instant search table, the code works correctly, I had made the query in such a way that it looked for me in the whole table, until then everything is fine, but now I need to ask the query with a filter ( REGIONAL ) and I think I have to do a nested query, but it does not work for me. The idea is that when doing the search, only the records that are of the REGIONAL of ORIENTE appear. P.D: I do not put all the code for saving, because as I said before it works correctly and for saving space.

The table I have is the following:

+--------+--------+----------+---------------+
|  PLACA | CIUDAD | REGIONAL | FECHA_TANQUEO |
+--------+--------+----------+---------------+
|ABC-123 |CALI    |OCCIDENTE | 2018-04-21    |
+--------+--------+----------+---------------+
|DEF-456 |BOGOTA  |ORIENTE   | 2018-04-20    |
+--------+--------+----------+---------------+
|GHI-789 |TUNJA   |ORIENTE   | 2018-04-20    |
+--------+--------+----------+---------------+

$q is the input variable where the characters are written to perform the search.

SELECT PLACA, CIUDAD, FECHA_TANQUEO, SUM(VALOR) AS SUMA
FROM combustible 
WHERE PLACA LIKE '%".$q."%' CIUDAD LIKE '%".$q."%' OR FECHA_TANQUEO LIKE '%".$q."%' AND REGIONAL = 'ORIENTE'
GROUP BY PLACA  ORDER BY 'SUMA' DESC"

The previous query shows me the cities of EAST but also shows me some cities of the WEST.

    
asked by Anderviver 23.04.2018 в 23:00
source

1 answer

3

Clearly, it is a problem in the prediction of logical operators. In addition, there are no AND operators | OR in the sentence.

SELECT PLACA, CIUDAD, FECHA_TANQUEO, SUM(VALOR) AS SUMA
FROM combustible 
WHERE 
-- necesitas definir cuáles condiciones pueden aceptarse juntas
(PLACA LIKE '%".$q."%' OR CIUDAD LIKE '%".$q."%' OR FECHA_TANQUEO LIKE '%".$q."%')
-- para que SIEMPRE se use este criterio, se deja por fuera del paréntesis
AND REGIONAL = 'ORIENTE'
-- fin del bloque problemático
GROUP BY PLACA 
ORDER BY 'SUMA' DESC"
    
answered by 23.04.2018 / 23:04
source