如何显示一次当前状态?

时间:2017-09-23 19:29:47

标签: php

我有一个家庭作业数据库,用于预订酒店。

我必须根据约会来检查预订。

例如:

2017年6月14日

查询返回房间号,并打印出可用性。

$selectedDay = $_POST['selectedDay'];
$select_date = "SELECT DISTINCT * FROM events WHERE '$selectedDay' BETWEEN start AND end - INTERVAL 1 DAY";
$result = mysqli_query($connection, $select_date);
echo "<h2>Free rooms in this date: <br/> $selectedDay</h2>";
if (mysqli_num_rows($result) > 0) {

    While ($row = mysqli_fetch_array($result)) {
        echo "<h3>" . $row['name'] . "</h3>";
        for ($i = 1; $i < 20; $i++) {
            if ($row['room' . $i] == '1') {
                echo "Room " . $i . ": <span style='color:red;'>busy</span> <br />";
            } else {
                echo "Room " . $i . ": <span style='color:green;'>free/span> <br />";
            }
        }
    }
} else {
    echo "No data for this date.";
}

我想要一个房间的整体列表,现在如果在所选日期有3位客人,则列表将被打印3次。

表格图片: https://ibb.co/jBVcik

1 个答案:

答案 0 :(得分:0)

我想你现在已经找到了自己的解决方案。如果您仍然在已经在SQL查询中完成工作的解决方案中存在问题,请查看:

SELECT group_concat(name) names, count(CASE room1 WHEN '1' THEN 1 END) r1, count(CASE room2 WHEN '1' THEN 1 END) r2, 
  count(CASE room3 WHEN '1' THEN 1 END) r3, count(CASE room4 WHEN '1' THEN 1 END) r4, count(CASE room5 WHEN '1' THEN 1 END) r5,
  count(CASE room6 WHEN '1' THEN 1 END) r6, count(CASE room7 WHEN '1' THEN 1 END) r7, count(CASE room8 WHEN '1' THEN 1 END) r8,
  count(CASE room9 WHEN '1' THEN 1 END) r9, count(CASE room10 WHEN '1' THEN 1 END) r10,count(CASE room11 WHEN '1' THEN 1 END) r11,
  count(CASE room12 WHEN '1' THEN 1 END) r12, count(CASE room13 WHEN '1' THEN 1 END) r13, count(CASE room14 WHEN '1' THEN 1 END) r14
FROM tbl where '20170823' between start and enddt-interval 1 day

结果是这样的

names               r1  r2  r3  r4  r5  r6  r7  r8  r9  r10 r11 r12 r13 r14
Charlie,Sally,Patty 0   1   2   0   0   0   0   0   0   0   0   0   0   0

您可以在此处使用它:http://rextester.com/HNDA58846

(我使用MySQL特定函数group_concat()来显示所选日期的所有来宾的姓名。)

如果您希望得到一个结果表(对于一系列预订日期),您可以稍微改为

SELECT dt,
   group_concat(name) names, count(CASE room1 WHEN '1' THEN 1 END) r1, count(CASE room2 WHEN '1' THEN 1 END) r2, 
   count(CASE room3 WHEN '1' THEN 1 END) r3, count(CASE room4 WHEN '1' THEN 1 END) r4, count(CASE room5 WHEN '1' THEN 1 END) r5,
   count(CASE room6 WHEN '1' THEN 1 END) r6, count(CASE room7 WHEN '1' THEN 1 END) r7, count(CASE room8 WHEN '1' THEN 1 END) r8,
   count(CASE room9 WHEN '1' THEN 1 END) r9, count(CASE room10 WHEN '1' THEN 1 END) r10,count(CASE room11 WHEN '1' THEN 1 END) r11,
   count(CASE room12 WHEN '1' THEN 1 END) r12, count(CASE room13 WHEN '1' THEN 1 END) r13, count(CASE room14 WHEN '1' THEN 1 END) r14
 FROM tbl 
 INNER JOIN (SELECT '20170823' dt UNION ALL SELECT '20170824' UNION ALL SELECT '20170825') dates
  ON dt between start and enddt-interval 1 day
 GROUP BY dt

WHERE子句更改为子查询的ON子句,dt现在从已加入的子查询(或其他表,如果您愿意)中拉出。结果:

dt          names               r1  r2  r3  r4  r5  r6  r7  r8  r9  r10 r11 r12 r13 r14
20170823    Sally,Charlie,Patty 0   1   2   0   0   0   0   0   0   0   0   0   0   0
20170824    Sally,Charlie,Patty 0   1   2   0   0   0   0   0   0   0   0   0   0   0
20170825    Sally,Patty         0   1   1   0   0   0   0   0   0   0   0   0   0   0

如果您的数据库中碰巧有一个计数表,那么一个连续数字的简单表就像这样:

id
---
 1
 2
 3
 4
 .
..
50

你可以用INNER JOIN用它来获取所有日期,当某个人 在你的一个房间里时:

...
LEFT JOIN (SELECT CAST('20170801' as date)+ interval id day dt FROM tally) dates
...

RIGHT JOIN如果您想了解某个日期范围内的所有其中的空间(空白与否)。