Check if a date and time (timestamp) represents the current day

8

I have a table with the following field: dh_envio (TIMESTAMP) and I want to mount a query to get all the records of this table in which the dh_envio is equal to the current date (TODAY).

I thought of it as follows:

WHERE dh.envio BEETWEEN '2014-02-17 00:00:00' AND '2014-02-17 23:59:59'

Is there any way to improve? how to use some MySQL constant? Because that way I need to get the date programmatically.

    
asked by anonymous 18.02.2014 / 02:13

3 answers

4

The alternative to doing this with pure SQL, without preparing the date before executing the query is

WHERE DATE(dh.envio) = DATE(NOW())

But do not do this . This automatic alternative has worse performance and should only be used if you are lazy and do not want to take advantage of query caching and are willing to do this by imagining that this query will calculate the TODD date of your table.

So the way you're doing now is performative. Unless you have something against it, stay that way.

    
18.02.2014 / 02:35
0

I always use the language because it does not depend on the processing of the DB, in this case you can do it as follows:

SELECT * FROM tabela WHERE dh.envio = (DIA ATUAL NA PROGRAMAÇÃO);

If it's PHP for example:

   $sql="SELECT * FROM tabela WHERE dh.envio =". NOW();

I hope I have helped.

    
05.03.2014 / 15:09
-1
WHERE DATE(dh.envio) = DATE(NOW())
    
06.07.2016 / 20:01