简体   繁体   中英

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.

I am facing the following error

the mySql exception is: MySQLSyntaxErrorException: You have an error in your SQL syntax; 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

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;

there are two table names as follows

1) roomtable-- that contain information about room such as room number,room type,facilties,room capacity etc

room table stucture or columns in roomtable

id | roomnumber |roomtype |no.ofbeds |room_capacity |facilities |basic_price |

2) roombook --contains information about the booking details such as customer name,type of room , room number,arrival date,departure date etc.

roombook stucture or columns in roombook

id |customername |room_number |room_type |arrival |departure |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

NOT EXIST is not prefixed by the column name. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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