如何在mysql中创建条件(使用'if')?

时间:2010-03-24 19:13:56

标签: mysql

此代码可以在特定日期内找到可用房间,但无法显示已预订和取消的房间

“酒店”有4个房间,其中1个已预订取消

所以即使我取消了,select方法也会给我3个结果。也许是因为第二个AND仍在运行。基本上我需要的是

  1. 检查房间是否在所选日期预订
  2. 如果已经预订,请检查是否已取消
  3. 如果已取消,或未预订显示。否则不是
  4. SELECT RoomNo, NightCost
    FROM room, room_types, booking
    WHERE typeid = fk1_typeid
    and double_bed=1
    and single_bed=0
    AND canceled = '1' in 
        (SELECT canceled
         from booking, room_booking
         where bookingid = fk2_bookingid)
    AND RoomNo not in 
        (SELECT fk1_RoomNo
         FROM room_booking
         WHERE '2010-04-02' between Check_in 
         and Check_Out or
         '2010-04-03' between Check_in 
         and Check_Out) ;
    

    我试图尽可能清楚,如果需要我会提供更多细节

2 个答案:

答案 0 :(得分:2)

SELECT  *
FROM    room
JOIN    room_types
ON      typeid = fk1_typeid
WHERE   double_bed = 1
        AND single_bed = 0
        AND roomNo NOT IN
        (
        SELECT  fk1_roomno
        FROM    room_booking
        WHERE   check_out >= '2010-04-02'
                AND check_in <= '2010-04-03'
                AND NOT canceled
        )

答案 1 :(得分:0)

  1. 如果

    ,则在2010-04-02和2010-04-04之间占用一个房间
    • 某人将在2010-04-03之前办理登机手续。
    • 并且有人不会在2010-04-02之前退房
    • 并且取消了room_booking。
  2. 当房间没有被占用时,房间是免费的


  3.  SELECT R.RoomNo, R.NightCost
       FROM room as R
       JOIN room_types as RT ON RT.typeid = R.fk1_typeid
     WHERE (R.single_bed=0 and R.double_bed=1)
       AND R.roomNo NOT IN (
            SELECT RB.fk1_roomno
              FROM room_booking as RB
             WHERE (RB.check_out >= '2010-04-02')
               AND (RB.check_in  <= '2010-04-03')
               AND NOT (RB.canceled=1)
        )     
    

    这正是Quassnoi在我面前所说的..感叹: - )