简体   繁体   English

查询酒店可用房间进行预订

[英]Query for available rooms in hotel for booking

I am getting an exception in the following mysql query pls help with this this problem by solving the query in detail.I am working on hotel management so I want to get only those rooms that are not booked or that are not in the booking table.我在以下 mysql 查询中遇到异常,请通过详细解决查询来帮助解决此问题。我从事酒店管理工作,因此我只想获取未预订或不在预订表中的房间。

I am facing the following error我面临以下错误

the mySql exception is: MySQLSyntaxErrorException: You have an error in your SQL syntax; mySql 异常是: MySQLSyntaxErrorException:您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'EXISTS (SELECT roomnumber FROM roombook WHERE Date(departure)<= Date('2020-03-26')' at line 1** here is the code the causing the error检查与您的 MySQL 服务器版本相对应的手册,以了解在第 1 行的“EXISTS (SELECT roomnumber FROM roombook WHERE Date(departure)<= Date('2020-03-26')”附近使用的正确语法**这里是编码导致错误

ResultSet rs=st.executeQuery("SELECT roomnumber, roomtype FROM roomtable where roomnumber NOT EXISTS (SELECT roomnumber FROM roombook WHERE Date(departure)<= Date('"+from_date+"')");

in the above code date(departure) is the date of departure of the customer that booked a particular room and date(from_date) id the input value of from input box of check availability form;在上面的代码中 date(departure) 是预订特定房间的客户的出发日期,而 date(from_date) id 是检查可用性表单的输入框的输入值;

there are two table names as follows有两个表名如下

1) roomtable-- that contain information about room such as room number,room type,facilties,room capacity etc 1) 房间表——包含房间的信息,如房间号、房间类型、设施、房间容量等

room table stucture or columns in roomtable房间表结构或房间表中的列

id |身份证 | roomnumber |roomtype |no.ofbeds |room_capacity |facilities |basic_price | roomnumber |roomtype |no.ofbeds |room_capacity |设施 |basic_price |

2) roombook --contains information about the booking details such as customer name,type of room , room number,arrival date,departure date etc. 2) roombook -- 包含有关预订详情的信息,例如客户姓名、房间类型、房间号、到达日期、出发日期等。

roombook stucture or columns in roombook房间簿结构或房间簿中的列

id |customername |room_number |room_type |arrival |departure |no.ofdays |no.ofadults |no.ofchild |no.ofPerson |total_price | id |客户姓名 |room_number |room_type |到达 |离开 |no.ofdays |no.ofadults |no.ofchild |no.ofPerson |total_price |

now using check availability I want to get the rooms that are not booked by the customers(or which are not in the roombook table) so for that I have used the above query , which causing an exception of type SQL syntax error exception, I am new to mysql and java thats why i am not able to find the correct way of writing a query现在使用检查可用性我想获取客户未预订的房间(或不在房间预订表中的房间),因此我使用了上面的查询,这导致了 SQL 语法错误异常类型的异常,我是mysql 和 java 新手,这就是为什么我无法找到编写查询的正确方法

NOT EXIST is not prefixed by the column name. NOT EXIST 没有以列名作为前缀。 Try the below (I also added an extra ")" at the end):试试下面的(我还在最后添加了一个额外的“)”):

ResultSet rs=st.executeQuery("SELECT roomnumber, roomtype FROM roomtable where NOT EXISTS (SELECT roomnumber FROM roombook WHERE Date(departure)<= Date('"+from_date+"'))");

NOTE: Your code may have a security flaw to allow SQL Injection, prefer using prepared statements with parameters instead of string concatenation.注意:您的代码可能存在允许 SQL 注入的安全漏洞,更喜欢使用带参数的准备好的语句而不是字符串连接。

MYSQL Exists: https://www.mysqltutorial.org/mysql-exists/ MYSQL 存在: https : //www.mysqltutorial.org/mysql-exists/

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

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