Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取可用的时间段MySQL PHP_Php_Mysql - Fatal编程技术网

获取可用的时间段MySQL PHP

获取可用的时间段MySQL PHP,php,mysql,Php,Mysql,此时,我可以查询大厅的所有预定时间,如下所示: SELECT TM.id , TM.start_date as start , TM.end_date as end , TM.start_time as start_time , TM.end_time as end_time , T.space FROM times as T JOIN times_meta as TM ON T.id = TM.time_id WHER

此时,我可以查询大厅的所有预定时间,如下所示:

SELECT TM.id
     , TM.start_date as start
     , TM.end_date as end
     , TM.start_time as start_time
     , TM.end_time as end_time
     , T.space 
  FROM times as T 
  JOIN times_meta as TM 
    ON T.id = TM.time_id 
 WHERE T.hall_id = 1 
   AND TM.start_date = "2019-09-20" 
 order 
    by start_time asc;
此返回结果为:

+----+---------------------+---------------------+---------------+
| id  | start      | end        | start_time | end_time | space  |
+-----+------------+------------+------------+----------+--------|
|  12 | 2012-09-20 | 2012-09-20 | 07:00:00   | 10:00:00 | full   |
|  2  | 2012-09-20 | 2012-09-20 | 10:00:00   | 11:00:00 | full   |
|  13 | 2012-09-20 | 2012-09-20 | 13:00:00   | 17:00:00 | full   |
|  15 | 2012-09-20 | 2012-09-20 | 17:00:00   | 18:00:00 | half   |
|  14 | 2012-09-20 | 2012-09-20 | 18:00:00   | 19:00:00 | full   |
|  16 | 2012-09-20 | 2012-09-20 | 19:00:00   | 21:00:00 | shared |
+-----+------------+------------+------------+----------+--------+
因此,假设营业时间是从
07:00:00
21:00:00

这意味着可用的时间范围是
11-13
17-18
19-21
。这是因为如果空间
已满
意味着无法共享时间段。对于
half
而言,可在双方之间共享,对于
shared
而言,最多可共享4方

表(次)

表(次)

我认为仅使用MySql可能会有点复杂,因此在php代码的帮助下获得所需的结果完全可以

我只想获得可用时间范围的列表,例如: 可用

11:00:00 - 13:00:00 - full
17:00:00 - 18:00:00 - half
19:00:00 - 21:00:00 - half / shared
更新: 要创建架构,请执行以下操作:

CREATE TABLE times (
id integer UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
space tinyint NOT NULL COMMENT ' 4 - full, 2 - half, 1 -shared ',
hall_id INT(6) UNSIGNED
);

CREATE TABLE times_meta (
id integer UNSIGNED AUTO_INCREMENT PRIMARY KEY,
start_date date NOT NULL,
end_date date NOT NULL,
start_time time NOT NULL,
end_time time NOT NULL,
time_id INT(6) UNSIGNED,
FOREIGN KEY (time_id) REFERENCES times(id)
);

INSERT INTO times 
        (name, space, hall_id)
VALUES 
        ('Booked by User 1', '4', '1'),
        ('Booked by User 2', '4', '1'),
        ('Booked by User 3', '4', '1'),
        ('Booked by User 4', '2', '1'),
        ('Booked by User 5', '4', '1'),
        ('Booked by User 5', '1', '1');

INSERT INTO times_meta 
        (start_date, end_date, start_time, end_time, time_id)
VALUES
        ('2019-09-20', '2019-09-20', '07:00:00', '10:00:00', '1'),
        ('2019-09-20', '2019-09-20', '10:00:00', '11:00:00', '2'),
        ('2019-09-20', '2019-09-20', '13:00:00', '17:00:00', '3'),
        ('2019-09-20', '2019-09-20', '17:00:00', '18:00:00', '4'),
        ('2019-09-20', '2019-09-20', '18:00:00', '19:00:00', '5'),
        ('2019-09-20', '2019-09-20', '19:00:00', '21:00:00', '6');

所以我尝试实现这个结果,得到了一个混合使用php和mysql的解决方案。 我使用mysql获取所有可用的一半或共享的房间列表,使用php获取所有可用的房间。
在php中,想法是首先将所有会议的开始时间和结束时间存储在两个单独的数组中,如果下次会议的开始时间不等于本次会议的结束时间,则它们之间有一个可用的时间段。
例如,我现在静态地创建了开始和结束数组

获取所有完整列表

<?php

$start = array('07:00:00','10:00:00','13:00:00','17:00:00','18:00:00','19:00:00');
$end = array('10:00:00','11:00:00','17:00:00','18:00:00','19:00:00','21:00:00');
$arrlength = count($start);
for($x = 1; $x < $arrlength-1; $x++) {
    if($start[$x]!=$end[$x-1]){
        echo $end[$x-1]."-".$start[$x]." - "."Full";
        echo "<br>";
    }
}
?>

@我试图用可能的数据集创建一个类似的数据库。我也不知道sqlfiddle在你这方面是否会一样。因此,我提供了测试代码,它应该生成一个最小的数据集。现在,你能帮我一下吗。如果需要更多信息,请告诉我。谢谢你的好意。stackoverflow上有许多人,如果问题中没有提供所需的全部信息,他们只是投了反对票并逃跑了,但没有提到原因。
CREATE TABLE times (
id integer UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
space tinyint NOT NULL COMMENT ' 4 - full, 2 - half, 1 -shared ',
hall_id INT(6) UNSIGNED
);

CREATE TABLE times_meta (
id integer UNSIGNED AUTO_INCREMENT PRIMARY KEY,
start_date date NOT NULL,
end_date date NOT NULL,
start_time time NOT NULL,
end_time time NOT NULL,
time_id INT(6) UNSIGNED,
FOREIGN KEY (time_id) REFERENCES times(id)
);

INSERT INTO times 
        (name, space, hall_id)
VALUES 
        ('Booked by User 1', '4', '1'),
        ('Booked by User 2', '4', '1'),
        ('Booked by User 3', '4', '1'),
        ('Booked by User 4', '2', '1'),
        ('Booked by User 5', '4', '1'),
        ('Booked by User 5', '1', '1');

INSERT INTO times_meta 
        (start_date, end_date, start_time, end_time, time_id)
VALUES
        ('2019-09-20', '2019-09-20', '07:00:00', '10:00:00', '1'),
        ('2019-09-20', '2019-09-20', '10:00:00', '11:00:00', '2'),
        ('2019-09-20', '2019-09-20', '13:00:00', '17:00:00', '3'),
        ('2019-09-20', '2019-09-20', '17:00:00', '18:00:00', '4'),
        ('2019-09-20', '2019-09-20', '18:00:00', '19:00:00', '5'),
        ('2019-09-20', '2019-09-20', '19:00:00', '21:00:00', '6');
<?php

$start = array('07:00:00','10:00:00','13:00:00','17:00:00','18:00:00','19:00:00');
$end = array('10:00:00','11:00:00','17:00:00','18:00:00','19:00:00','21:00:00');
$arrlength = count($start);
for($x = 1; $x < $arrlength-1; $x++) {
    if($start[$x]!=$end[$x-1]){
        echo $end[$x-1]."-".$start[$x]." - "."Full";
        echo "<br>";
    }
}
?>
SELECT 
   TM.start_time as start_time
 , TM.end_time as end_time
 ,
CASE 
    WHEN T.space = 2 THEN 'Half'
    WHEN T.space = 1 THEN 'Half/Shared'
END AS 'TYPE'
  FROM times as T 
  JOIN times_meta as TM 
    ON T.id = TM.time_id 
 WHERE T.hall_id = 1 
   AND TM.start_date = "2019-09-20"
   AND T.space != 4
 order 
    by start_time asc;