Error in MySQL query

0

When I try to run, the next query does not throw anything at me. someone could advise me, thanks.

SELECT * FROM bd.tabla WHERE interv='15seg' 
    IF(interv='15seg' 
    AND fecha_hora='2016-08-02 00:00:00' 
    AND fecha_hora >= '2016-08-18 23:59:59')
    SET hora between '00:00:00' and '00:00:00' 
    ELSE (interv='5HZ,15seg' 
    AND and fecha_hora >= '2016-08-02 00:00:00' 
    AND fecha_hora <= '2016-08-18 23:59:59' 
    and estacion in ('est1','est2','est3') 
and hora between '00:00:00' and '03:59:59')
    
asked by Houdini 18.08.2016 в 22:29
source

3 answers

7

This

AND fecha_hora='2016-08-02 00:00:00' 
AND fecha_hora >= '2016-08-18 23:59:59')

they sound at the entrance two AND incompatible with each other because it tells the system to look for two dissociated date ranges with a AND , which will never happen, therefore nothing comes back.

Maybe if you modify it in

AND fecha_hora>='2016-08-02 00:00:00' 
AND fecha_hora <= '2016-08-18 23:59:59')

sounds more logical.

Maybe there are more things about the content of the table itself, but this jumps into your eyes immediately.

    
answered by 18.08.2016 в 23:06
5

If what you need is to search between dates, it is best to use between .

It's easier to read and the system will know what to do, it's a function.

Example

WHERE
.
.
AND

fecha_hora BETWEEN '2016-08-02 00:00:00' AND '2016-08-18 23:59:59'
.
.
    
answered by 19.08.2016 в 09:40
2

Try putting dates with BETWEEN

SELECT * FROM bd.tabla WHERE interv='15seg' 
    IF(interv='15seg' 
    AND fecha_hora BETWEEN '2016-08-02 00:00:00' 
    AND '2016-08-18 23:59:59')
    SET hora between '00:00:00' and '00:00:00' 
    ELSE (interv='5HZ,15seg' 
    AND fecha_hora BETWEEN '2016-08-02 00:00:00' 
    AND '2016-08-18 23:59:59' 
    and estacion in ('est1','est2','est3') 
and hora between '00:00:00' and '03:59:59')

BETWEEN is a very useful operator to use within the WHERE clause, to specify a range of inclusive values. It is normally used with dates but can also be used with strings and numbers.

    
answered by 19.08.2016 в 10:17