简体   繁体   English

MySQL查询预订系统-如果已经预订了房间,则删除条目

[英]MySQL query booking system - remove entry if room is already booked

I have been banging my head on this (im a newbie in MySQL, be patient) for two days and i cannot get around this query i want to make... First the data: 我已经为此花了两天时间(我是MySQL的新手,请耐心等待),我无法绕开我要查询的查询...首先是数据:

I have two tables, one for the rooms like this: 我有两张桌子,一张用于这样的房间:

  • table ROOMS 桌子的房间
  • id_room | id_room | tipe_room | tipe_room | view_room view_room
  • 1.1 | 1.1 | Single | 单身| Sea
  • 1.2 | 1.2 | Single | 单身| Country 国家
  • 1.3 | 1.3 | Double | 双| Sea
  • 1.4 | 1.4 | Double | 双| Country 国家
  • 1.5 | 1.5 | Single | 单身| Sea

  • table RESERVATIONS 表预订

  • id_reservation | id_reservation | client_number | client_number | n_room | 房间 check_in | 签入| check_out 查看
  • 1 | 1 | 1 | 1 | 1.1 | 1.1 | 2012-02-20 | 2012-02-20 | 2012-02-24 2012-02-24

If a person wants to book a room with tipe_room=Single and view_room=Sea and check_in date=2012-02-15 and check_out date=2012-02-20 i want it to return the id_room available (1.1 and 1.5). 如果某人要预订一个带有tipe_room = Singleview_room = Seacheck_in date = 2012-02-15check_out date = 2012-02-20的房间,我希望它返回可用的id_room(1.1和1.5)。

Now if a person wants to book a room with tipe_room=Single and view_room=Sea and check_in date=2012-02-19 and check_out date=2012-02-23 i want it to return the id_room available (only 1.5). 现在,如果某人要预订一个带有tipe_room = Singleview_room = Seacheck_in date = 2012-02-19check_out date = 2012-02-23的房间,我希望它返回可用的id_room(仅1.5)。

I have been trying around JOIN conditions but when conditions are met it excludes all rooms... 我一直在尝试JOIN条件,但是当条件满足时,它将排除所有房间...

How can I do this, please? 请问我该怎么做?

Best Regards, Alex 最好的问候,亚历克斯

This query should check for all four cases (A,B,C and D) of occupancy: 此查询应检查所有四种情况(A,B,C和D)的占用情况:

  check_in     check_out
      |=TEST=PERIOD=|

  |--A--| |--B--| |--C--|  <- check_in<period_end AND check_out>period_start
    |--------D--------|    <- check_in<period_start AND check_out>period_end

This is complete query: 这是完整的查询:

SELECT `id_room` FROM `rooms` 
WHERE `tipe_room`='Single' AND `view_room`='Sea' 
AND NOT EXISTS 
(
SELECT * FROM `reservations` WHERE `n_room`=`rooms`.`id_room` 
AND ((`check_in`<'2012-02-20' AND `check_out`>'2012-02-15') 
 OR (`check_in`<'2012-02-15' AND `check_out`>'2012-02-20'))
)

Try this Query 试试这个查询

select * from room where room.view = :view and room.type = :type
and (select count(*) from reservation r where r.room_id = room.id and 
(r.checkout > :checkin and r.checkin  < :checkout) or
(r.checkin  < :checkin and r.checkout > :checkin))  = 0

The main idea here is to select the room and then check that it is not already booked in the sub select. 这里的主要思想是选择房间,然后检查子选择中是否尚未预订。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM