我有一个表"Created"作为日期时间.
我正在尝试查询以检查Created值的时间是否在两次之间.
第一行的创建日期时间是'2013-07-01 00:00:00.000'(午夜),我正在尝试查询时间在晚上11点到早上7点之间的项目.
select *
from MyTable
where CAST(Created as time) between '23:00:00' and '06:59:59'
Run Code Online (Sandbox Code Playgroud)
但是没有返回任何结果.
我需要将时间转换为日期时间吗?
Jon*_*eet 38
我怀疑你要检查它是在晚上11点之后还是在早上7 点之前:
select *
from MyTable
where CAST(Created as time) >= '23:00:00'
or CAST(Created as time) < '07:00:00'
Run Code Online (Sandbox Code Playgroud)
select *
from MyTable
where CAST(Created as time) not between '07:00' and '22:59:59 997'
Run Code Online (Sandbox Code Playgroud)
小智 7
我有一个非常相似的问题,想分享我的解决方案
给定此表(所有 MySQL 5.6):
create table DailySchedule
(
id int auto_increment primary key,
start_time time not null,
stop_time time not null
);
Run Code Online (Sandbox Code Playgroud)
选择给定时间x (hh:mm:ss)介于开始时间和停止时间之间的所有行。包括第二天。
注意:替换为您喜欢的NOW()任何时间x
SELECT id
FROM DailySchedule
WHERE
(start_time < stop_time AND NOW() BETWEEN start_time AND stop_time)
OR
(stop_time < start_time AND NOW() < start_time AND NOW() < stop_time)
OR
(stop_time < start_time AND NOW() > start_time)
Run Code Online (Sandbox Code Playgroud)
结果
给定
id: 1, start_time: 10:00:00, stop_time: 15:00:00
id: 2, start_time: 22:00:00, stop_time: 12:00:00
Run Code Online (Sandbox Code Playgroud)
NOW = 09:00:00:2NOW = 14:00:00:1NOW = 11:00:00:1,2NOW = 20:00:00:无| 归档时间: |
|
| 查看次数: |
89680 次 |
| 最近记录: |