获取时间戳内的记录,但不是特定于日期

时间:2016-10-19 11:02:04

标签: mysql ruby-on-rails ruby database

我想获取在晚上11:02到下午4:04之间登录的用户记录,日期并不重要。

我尝试过类似的事情:

User.where("TIME(created_at) BETWEEN '11:02:00' AND '04:04:00'")

但它没有用。非常感谢任何帮助。

4 个答案:

答案 0 :(得分:4)

函数之间的mysql与你期望的不同。

  

between函数期望第一个参数为最小值,第二个参数为最大值。请参阅文档:http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between

试试这个:

User.where("(TIME(created_at) BETWEEN '23:00:00' AND '23:59:59' OR TIME(created_at) BETWEEN '00:00:00' AND '03:59:59')")

根据您的更新,它将是这样的:

User.where("(TIME(created_at) BETWEEN '23:02:00' AND '23:59:59' OR TIME(created_at) BETWEEN '00:00:00' AND '04:03:59')")

答案 1 :(得分:1)

试试这个:

User.where.not("Hour(created_at) >= '16:04' AND Hour(created_at) <= '11:02'")

答案 2 :(得分:0)

尝试此查询:

User.where("DATE_FORMAT(`created_at`,'%H:%i:%s') BETWEEN '11:00:00' AND '04:00:00'");

答案 3 :(得分:0)

select * from time_table 
   where HOUR(time_column) BETWEEN 6 AND 11