Finding free blocks of time in mysql and php?(在 mysql 和 php 中寻找空闲的时间块?)
问题描述
我在 mysql 中有一个这样的表:
I have a table in mysql like this:
+------------+------------+------------+------------+
| 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 |
+------------+------------+------------+------------+
我尝试提取用户时间的时间范围我在此处找到并举例,但我无法在几个小时内完成这项工作
and I try to extract the time range of the time of users I found and example here, but I can't make that work on hours
我试过查询:
$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 的范围,其中包含约会之间的空闲块像这样:
I need to create a range since 08:00 to 21:00 with the free blocks between the appointments like this:
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 作为开始和结束日期.
Also you must have the 21:00:00 in the table or you wont be able to get the last time difference. i entered 21:00:00 as start and end date in the table.
这是修改后的查询
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 |
+----------------------+---------------------------------+
从这个查询中学习的要点是
The points to learn from this query are
- Timediff 函数的使用.timediff('结束时间','开始时间')
- 用大号加入
- 避免使用长偏移量连接的第一条记录,并限制从 1 而不是零开始
- IFNULL 用法 ifnull('if here come null','select this then')
这篇关于在 mysql 和 php 中寻找空闲的时间块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 mysql 和 php 中寻找空闲的时间块?
基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01