完全迷失在这里。我有一个带约会表的mysql数据库。对此重要的是它有一个start_date和end_date
假设我需要找到下一个可用的30分钟时段。需要统计周一至周五和上午7点至晚上7点
基本上我需要一种方法来自动执行此过程。我的SQL查询会是什么样的?
从日历中选择*,其中end_date <now和????丢失
考虑到我不熟练的SQL,但我只是试图解决问题。这样的事情可以解决问题吗?
SELECT *, TIMEDIFF(end_date, start_date) as time_diff FROM calendar WHERE start_date > NOW() AND (TIME_TO_SEC(TIMEDIFF(end_date, start_date)) / 60) > 30;
如果您添加ORDER BY start_date
和LIMIT 1
,您将获得第一场比赛。
如果您不担心约会表中的“空”插槽,您可以设置一个程序,定期预先分配插槽到“预订范围”(例如,下个月末)并删除过去“空的“插槽。
create table `appointment` (
`slot` varchar (90),
`remarks` varchar (180)
);
insert into `appointment` (`slot`, `remarks`) values('2016-03-01 08:00','');
insert into `appointment` (`slot`, `remarks`) values('2016-03-01 09:00','');
insert into `appointment` (`slot`, `remarks`) values('2016-03-01 10:00','');
insert into `appointment` (`slot`, `remarks`) values('2016-03-01 11:00','Tennis with Mark');
insert into `appointment` (`slot`, `remarks`) values('2016-03-01 12:00','Lunch at home');
insert into `appointment` (`slot`, `remarks`) values('2016-03-01 13:00','Programming');
insert into `appointment` (`slot`, `remarks`) values('2016-03-01 14:00','');
insert into `appointment` (`slot`, `remarks`) values('2016-03-01 15:00','');
insert into `appointment` (`slot`, `remarks`) values('2016-03-01 16:00','');
insert into `appointment` (`slot`, `remarks`) values('2016-03-01 17:00','');
...
...
delete from `appointment` where slot < '2016-02-01' and remarks='';
查找示例中的第一个约会的查询很简单:
select * from `appointment` where `slot` > '2016-03-01' and `remarks` = ''
但是您可以创建更复杂的搜索模式:
select * from `appointment`
where left(`slot`,10) between '2016-03-01' and '2016-03-01'
and right(`slot`,5) between '15:00' and '17:00'
and `remarks` = ''
这不是技术上的答案,只是因为问题不是技术问题,而是逻辑问题。我前段时间处于相同的状况,我很快意识到,由于桌子的大小不断增加,以及所需的资源,我在整个约会表中搜索只是极端。另外,我不得不处理不同的时间间隔,例如有10分钟的约会,5分钟的约会和15分钟的约会。
因此我在逻辑上接近这个,我得出以下解决方案:
以这种方式,数据库的作业是相同的,因为它每次都具有相同的工作负载。