[英]MySql: can this query be more efficient?
I have a table called TIMINGS
我有一张名为
TIMINGS
的表
----------------create---------------- - - - - - - - - 创建 - - - - - - - -
CREATE TABLE TIMINGS(
SNO TINYINT UNIQUE NOT NULL AUTO_INCREMENT,
TIMINGTYPE VARCHAR(20) PRIMARY KEY,
STARTTIME TIME NOT NULL,
ENDTIME TIME NOT NULL
)
------------------ insert --------------------- - - - - - - - - - 插入 - - - - - - - - - - -
INSERT INTO TIMINGS(TIMINGTYPE, STARTTIME, ENDTIME) VALUES
('BREAKFAST','08:34:00','10:00:00'),
('LUNCH','11:30:00','13:30:00'),
('DINNER','19:00:00','20:30:00');
so i want to know TIMINGTYPE according to current time and if the next TIMINGTYPE所以我想根据当前时间和下一个 TIMINGTYPE 知道 TIMINGTYPE
ex.前任。 : if the current time between breakfast start and breakfast end then it should return 'BREAKFAST' and the next TIMINGTYPE
:如果早餐开始和早餐结束之间的当前时间那么它应该返回'BREAKFAST'和下一个TIMINGTYPE
Expected output ex.:预期输出例如:
----------------------------------------------------------
CURRENTTIME | NEXT
-----------------------------------------------------------
1. BREAKFAST | LUNCH
-----------------------------------------------------------
so i made a query which is working according to my requirement:所以我做了一个根据我的要求工作的查询:
---------------------- select ---------------------------- - - - - - - - - - - - 选择 - - - - - - - - - - - - - - ——
SELECT (
CASE
WHEN DATE_FORMAT(CONVERT_TZ(NOW(),'+00:00','+05:30') ,'%H:%i:%s' ) BETWEEN (select STARTTIME from TIMINGS where TIMINGTYPE = 'BREAKFAST') AND (select ENDTIME from TIMINGS where TIMINGTYPE = 'BREAKFAST') THEN 'BREAKFAST'
WHEN DATE_FORMAT(CONVERT_TZ(NOW(),'+00:00','+05:30') ,'%H:%i:%s' ) BETWEEN (select STARTTIME from TIMINGS where TIMINGTYPE = 'LUNCH') AND (select ENDTIME from TIMINGS where TIMINGTYPE = 'LUNCH') THEN 'LUNCH'
WHEN DATE_FORMAT(CONVERT_TZ(NOW(),'+00:00','+05:30') ,'%H:%i:%s' ) BETWEEN (select STARTTIME from TIMINGS where TIMINGTYPE = 'DINNER') AND (select ENDTIME from TIMINGS where TIMINGTYPE = 'DINNER') THEN 'DINNER'
ELSE 'NOT AVAILABLE'
END
) as CURRENTTIME,
(
CASE
WHEN DATE_FORMAT(CONVERT_TZ(NOW(),'+00:00','+05:30') ,'%H:%i:%s' ) BETWEEN (select ENDTIME from TIMINGS where TIMINGTYPE = 'BREAKFAST') AND (select STARTTIME from TIMINGS where TIMINGTYPE = 'LUNCH') THEN 'LUNCH'
WHEN DATE_FORMAT(CONVERT_TZ(NOW(),'+00:00','+05:30') ,'%H:%i:%s' ) BETWEEN (select ENDTIME from TIMINGS where TIMINGTYPE = 'LUNCH') AND (select STARTTIME from TIMINGS where TIMINGTYPE = 'DINNER') THEN 'DINNER'
ELSE 'BREAKFAST'
END) as NEXT
So my question is can i make it more efficient?所以我的问题是我可以让它更有效率吗? If yes then how!
如果是,那么如何!
NOTE: I'm using MySql注意:我正在使用 MySql
Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks!
谢谢!
You could just do:你可以这样做:
select t.*
from timings t
where
(current_time between starttime endtime) -- on-going
or (current_time >= endttime) -- next
order by starttime
limit 1
The logic is to search for records that are on-going or future, and then to sort them by date and keep the earliest.逻辑是搜索正在进行或将来的记录,然后按日期对其进行排序并保留最早的记录。
Note that the use of current_time
greatly simplifies the time logic.请注意,使用
current_time
大大简化了时间逻辑。
Edit编辑
After the question was updated: you can use union all
:问题更新后:您可以使用
union all
:
-- on-going
(select 'current', timingtype from timings where current_time between starttime and endtime)
union all
-- upcoming
(select 'upcomong', timingtype from timings where current_time > endtime order by starttime limit 1)
If you want the results on the same row:如果您希望结果在同一行:
select
(select timingtype from timings where current_time between starttime and endtime) current,
(select timingtype from timings where current_time > endtime order by starttime limit 1) upcoming
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.