具有左连接和时间条件的MySQL查询

时间:2016-05-15 22:23:43

标签: mysql sql database

我有以下表格:

mysql> select * from room;
+----+----------+
| ID | NAME     |
+----+----------+
|  1 | Room 101 |
|  2 | Room 102 |
|  3 | Room 103 |
+----+----------+


mysql> select * from booking;
+---------+---------------------+---------------------+----------------+
| ROOM_ID | START               | END                 | GUEST          |
+---------+---------------------+---------------------+----------------+
|       1 | 2016-04-01 00:00:00 | 2016-04-30 00:00:00 | Dorian Gray    |
|       1 | 2016-05-03 00:00:00 | 2016-05-20 00:00:00 | Vanessa Ives   |
|       1 | 2016-05-21 00:00:00 | 2016-05-30 00:00:00 | Ethan Chandler |
|       2 | 2016-05-06 00:00:00 | 2016-05-18 00:00:00 | John Clare     |
+---------+---------------------+---------------------+----------------+

对于每个房间,我想获得有关其预订状态的其他信息。如果已预订,我想显示预订状态。如果没有预订,则必须显示NULL

如果我从基本查询开始..

mysql> select * from room r, booking b where r.id = b.room_id;
+----+----------+---------+---------------------+---------------------+----------------+
| ID | NAME     | ROOM_ID | START               | END                 | GUEST          |
+----+----------+---------+---------------------+---------------------+----------------+
|  1 | Room 101 |       1 | 2016-04-01 00:00:00 | 2016-04-30 00:00:00 | Dorian Gray    |
|  1 | Room 101 |       1 | 2016-05-03 00:00:00 | 2016-05-20 00:00:00 | Vanessa Ives   |
|  1 | Room 101 |       1 | 2016-05-21 00:00:00 | 2016-05-30 00:00:00 | Ethan Chandler |
|  2 | Room 102 |       2 | 2016-05-06 00:00:00 | 2016-05-18 00:00:00 | John Clare     |
+----+----------+---------+---------------------+---------------------+----------------+

它运作正常,但这不是我想要的......让我们看看左连接......

mysql> select * from room r left join booking b on r.id = b.room_id;
+----+----------+---------+---------------------+---------------------+----------------+
| ID | NAME     | ROOM_ID | START               | END                 | GUEST          |
+----+----------+---------+---------------------+---------------------+----------------+
|  1 | Room 101 |       1 | 2016-04-01 00:00:00 | 2016-04-30 00:00:00 | Dorian Gray    |
|  1 | Room 101 |       1 | 2016-05-03 00:00:00 | 2016-05-20 00:00:00 | Vanessa Ives   |
|  1 | Room 101 |       1 | 2016-05-21 00:00:00 | 2016-05-30 00:00:00 | Ethan Chandler |
|  2 | Room 102 |       2 | 2016-05-06 00:00:00 | 2016-05-18 00:00:00 | John Clare     |
|  3 | Room 103 |    NULL | NULL                | NULL                | NULL           |
+----+----------+---------+---------------------+---------------------+----------------+

但是,它显示重复...让我们尝试更多条件:

mysql> select * from room r left join booking b on r.id = b.room_id where b.start<now() and b.end>now();
+----+----------+---------+---------------------+---------------------+--------------+
| ID | NAME     | ROOM_ID | START               | END                 | GUEST        |
+----+----------+---------+---------------------+---------------------+--------------+
|  1 | Room 101 |       1 | 2016-05-03 00:00:00 | 2016-05-20 00:00:00 | Vanessa Ives |
|  2 | Room 102 |       2 | 2016-05-06 00:00:00 | 2016-05-18 00:00:00 | John Clare   |
+----+----------+---------+---------------------+---------------------+--------------+

好的......但是错了,因为'103室'没有显示(没有预订)......

我想得到这样的东西:

+----+----------+---------+---------------------+---------------------+--------------+
| ID | NAME     | ROOM_ID | START               | END                 | GUEST        |
+----+----------+---------+---------------------+---------------------+--------------+
|  1 | Room 101 |       1 | 2016-05-03 00:00:00 | 2016-05-20 00:00:00 | Vanessa Ives |
|  2 | Room 102 |       2 | 2016-05-06 00:00:00 | 2016-05-18 00:00:00 | John Clare   |
|  3 | Room 103 |    NULL | NULL                | NULL                | NULL         |
+----+----------+---------+---------------------+---------------------+--------------+

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

我不明白你为什么要使用约会时间来存储日期...但无论如何......

SELECT * 
  FROM room r 
  LEFT 
  JOIN booking b 
    ON b.room_id = r.id
   AND CURDATE() BETWEEN b.start AND b.end;

答案 1 :(得分:0)

下面的查询应该为您提供您正在寻找的结果。 SELECT * FROM room r 将为您提供每个房间号码和 SELECT * FROM预订b WHERE CURDATE()BETWEEN b.start AND b.end 将为您提供包含的记录今天的日期在您的开始日期和结束日期之内。然后,您将使用子查询 ON r.id = b.room_id LEFT JOIN 主要查询。

SELECT * FROM room r LEFT JOIN 
(SELECT * FROM booking b WHERE CURDATE() BETWEEN b.start AND b.end) b ON r.id = b.room_id;