ICode9

精准搜索请尝试: 精确搜索
首页?>?数据库> 文章详细

在mysql和php中查找空闲时间块?

2019-09-23 19:15:45??阅读:580??来源: 互联网

标签:php?mysql?time?appointment


我在mysql中有一个像这样的表:

+------------+------------+------------+------------+ 
|     date   |   user_id  | start_hour | end_hour   | 
+------------+------------+------------+------------+ 
| 2010-12-15 |         20 | 08:00:00   | 08:15:00   | 
| 2010-12-15 |         20 | 14:00:00   | 14:30:00   | 
| 2010-12-15 |         20 | 17:00:00   | 17:45:00   | 
+------------+------------+------------+------------+ 

我试着提取用户时间的时间范围
我找到了例子here,但我无法在几小时内完成这项工作

我尝试了查询:

 $sql="
SELECT a.end_hour AS 'Available From', Min(b.start_hour) AS 'To' 

FROM ( 
SELECT  0 as date, '08:00:00' as start_hour,'08:00:00' as end_hour 
UNION SELECT date, start_hour, end_hour FROM table
) 

AS a JOIN 
( SELECT  date, start_hour, end_hour  FROM table 
UNION SELECT 0,  '21:00:00' as start_hour, '22:00:00' as end_hour
) AS b ON

a.date=b.date AND  a.user_id=b.user_id AND a.end_hour < b.start_hour WHERE  
a.date='$date'  AND a.user_id='$user_id' GROUP BY a.end_hour 
HAVING a.end_hour < Min(b.start_hour);"; 

我需要在08:00到21:00之间创建一个范围,并在约会之间设置免费区块
像这样:

free time
08:15:00 to 14:00:00
14:30:00 to 17:00:00
17:45:00 to 21:00:00

解决方法:

试试这个查询

SELECT
  a.id,
  a.start_hour,
  a.end_hour,
  TIMEDIFF(la.start_hour, a.end_hour) as `Free Time`
FROM appointment as a
  LEFT JOIN(SELECT * FROM appointment LIMIT 1,18446744073709551615) AS la
    ON la.id = a.id + 1
  LEFT JOIN (SELECT * FROM appointment) AS ra ON a.id = ra.id

这将显示这些结果

+---------------------------------------------+
| id | start_hour BY | end_hour   | Free Time |
|----+---------------|------------------------|
|  1 |   08:00:00    |  08:15:00  | 05:45:00  |
|  2 |   14:00:00    |  14:30:00  | 02:30:00  |
|  3 |   17:00:00    |  17:45:00  | 03:15:00  |
|  4 |   21:00:00    |  21:00:00  | (NULL)    |
+--------------------+------------------------+ 

此外,您必须在表中21:00:00,否则您将无法获得最后的时差.我在表格中输入21:00:00作为开始和结束日期.

EDITED

这是修改后的查询

SELECT
  a.id,
  a.end_hour AS `Free time Start`,
  IFNULL(la.start_hour,a.end_hour) AS `Free Time End`,
  IFNULL(TIMEDIFF(la.start_hour, a.end_hour),'00:00:00') AS `Total Free Time`
FROM appointment AS a
  LEFT JOIN (SELECT * FROM appointment LIMIT 1,18446744073709551615) AS la
    ON la.id = (SELECT MIN(id) FROM appointment where id > a.id LIMIT 1)      

结果是

+--------------------------------------------------------+
| id | Free time Start | Free Time End | Total Free Time |
|----+-----------------|---------------------------------|
|  1 |   08:15:00      | 14:00:00      |    05:45:00     |
|  2 |   14:30:00      | 17:00:00      |    02:30:00     |
|  3 |   17:45:00      | 21:00:00      |    03:15:00     |
|  4 |   21:00:00      | 21:00:00      |    00:00:00     |
+----------------------+---------------------------------+  

从这个查询中学习的要点是

> Timediff功能使用. timediff(‘结束时间’,’开始时间’)
>加入数字上限
>避免使用长偏移量的连接中的第一个记录,并从1开始而不是从零开始限制
> IFNULL使用ifnull(‘如果这里为空’,’选择此然后’)



标签:php,mysql,time,appointment

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有