【问题标题】:Multiple Joins in SQLSQL 中的多个连接
【发布时间】:2018-04-12 01:55:22
【问题描述】:

我一直在疯狂地尝试使这个 SQL 工作。

基本上,我要做的是按房间类型计算当前预订了多少房间。到目前为止,我已经能够使用 table1 和 table2 的连接语句来获取当前日期的总体预订房间总数,但是我找不到链接 4 个表并基本上创建可用房间列表的方法按 roomtype_id 类型。最让我困惑的部分是 table4 每个房间都有一个新条目,所以在这个例子中 roomtype_id "1" 有两个房间,而 roomtype_id "2" 有一个房间。

我想得到的最终结果是:
Green Room 有 1 间空房,
Blue Room 有 1 间客房。

(假设今天是 8 月 11 日,因为在 11 日有一个房间类型“1”的预订)

我有 4 张桌子,

表 1:
bookings_id    room_type_id
1                    1

表2:
booking_id      booking_date
1                     2013-08-11

表 3:
roomtype_id    type_name
1                    绿色房间
2                     蓝色房间

table4:
room_id          roomtype_id
1                    1
2                     1
3                     2

希望我没有太困惑。

【问题讨论】:

    标签: mysql join


    【解决方案1】:

    试试这个..

    select * from table1 join
    table2 on table1.bookings_id  = table2.bookings_id join
    table4 on table1.room_type_id  = table4.roomtype_id join
    table3 on table1.room_type_id  = table3.roomtype_id and table3.roomtype_id  = table4.roomtype_id 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-16
      • 2023-03-24
      • 2014-12-31
      • 2010-11-22
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多