这是我的数据库image。我想获取clockin_time> 10:00 AM的所有记录。你能帮我吗?
答案 0 :(得分:1)
给予
+----+----------+
| id | ts |
+----+----------+
| 1 | 09:00 AM |
| 2 | 10:00 AM |
| 3 | 11:00 PM |
| 4 | 09:00 PM |
| 5 | 09:00 |
| 6 | 22:00 |
+----+----------+
然后
select id,ts
from t
where case when instr(upper(ts),'AM') > 0 then
cast(trim(replace(ts,'AM','')) as time)
when instr(upper(ts),'PM') > 0 then
case when cast(substring_index(ts,':',1) as integer) < 12 then
addtime(cast(trim(replace(ts,'PM','')) as time),'12:00:00')
else
cast(trim(replace(ts,'PM','')) as time)
end
else
cast(trim(ts) as time)
end > cast('10:00' as time);
产生
+----+----------+
| id | ts |
+----+----------+
| 3 | 11:00 PM |
| 4 | 09:00 PM |
| 6 | 22:00 |
+----+----------+
这里没有什么复杂的,并且代码试图掩盖似乎是一种相当随机的时间存储方式。
答案 1 :(得分:0)
如果clockin_time确实是VARCHAR(这是我假设的),则在选择查询中将VARCHAR更改为DATETIME,然后可以对其进行比较。参见下面的操作方法。
SELECT * FROM tbl_clock WHERE STR_TO_DATE(clockin_time, '%h:%i %p') > '10:00:00';