Seleccionar registros desde AHORA () -1 Día


¿Hay alguna forma en una sentencia MySQL de ordenar registros (a través de un sello de fecha) por >= NOW() -1 para que se seleccionen todos los registros desde el día antes de hoy hasta el futuro?

Author: Vadim Kotov, 2011-12-17

6 answers

A juzgar por la documentación de para las funciones de fecha/hora , debería ser capaz de hacer algo como:

SELECT * FROM FOO
WHERE MY_DATE_FIELD >= NOW() - INTERVAL 1 DAY
 174
Author: Jon Skeet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-17 11:39:19

Tenga en cuenta que el resultado puede ser ligeramente diferente de lo que espera.

NOW() devuelve un DATETIME.

Y INTERVAL funciona como nombrado, por ejemplo, INTERVAL 1 DAY = 24 hours.

Así que si su script se cron'd para ejecutar en 03:00, se perderá el first three hours of records from the 'oldest' day.

Para conseguir el uso del día entero CURDATE() - INTERVAL 1 DAY. Esto volverá al principio del día anterior independientemente de cuándo se ejecute el script.

 35
Author: William Dan Terry,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-01 04:16:29

Ya casi estás ahí: es NOW() - INTERVAL 1 DAY

 14
Author: dasblinkenlight,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-17 11:40:57

Claro que puedes:

SELECT * FROM table
WHERE DateStamp > DATE_ADD(NOW(), INTERVAL -1 DAY)
 7
Author: Marco Miltenburg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-17 11:43:44

Cuando el campo de búsqueda es marca de tiempo y desea encontrar registros de 0 horas ayer y 0 hora hoy use construcción

MY_DATE_TIME_FIELD between makedate(year(now()), date_format(now(),'%j')-1) and makedate(year(now()), date_format(now(),'%j'))  

En su lugar

 now() - interval 1 day
 0
Author: Michael de Oz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-20 08:15:30

No vio ninguna respuesta correctamente usando DATE_ADD o DATE_SUB:

Restar 1 día de NOW()

...WHERE DATE_FIELD >= DATE_SUB(NOW(), INTERVAL 1 DAY)

Añadir 1 día desde NOW()

...WHERE DATE_FIELD >= DATE_ADD(NOW(), INTERVAL 1 DAY)
 0
Author: Andrew Atkinson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-03 13:48:41