在sql中选择日期时间戳

问题描述 投票:-3回答:1

在我的sql表中,我有一个标题为“fld_datetimestamp”的列,其值为,

2017-12-12 09:41:20

我从用户那里得到变量'hours'。 (蟒蛇)

说,我目前的时间戳是2017-12-12 10:00:00。我的用户给了我6(小时)的值。

基于此,我的查询应该能够从2017-12-12 04:00:00 to 2017-12-12 10:00:00.过滤fld_datetimestamp

从现在到现在六个小时

我该如何编写查询?每次用户给出新的小时值时,都应该动态实现这一点。

mysql sql
1个回答
-1
投票

MySQL的:

select col1_date, col2 
  from table_name 
where col1_time between date_add(date_format(now(),'%Y-%m-%d %H:00:00'),interval -6 hour)  
      and date_format(now(),'%Y-%m-%d %H:00:00');
© www.soinside.com 2019 - 2024. All rights reserved.