gpt4 book ai didi

mysql - 管理预订系统中的日期

转载 作者:行者123 更新时间:2023-11-29 17:08:44 25 4
gpt4 key购买 nike

大家好,我正在用 Java 创建一个简单的 Web 应用程序,我应该在其中预订房间。现在我的问题是能否在同一时期预订房间。让我更好地解释一下,我有 2 种类型的房间,卧室 1 和卧室 2。如果用户1于2018年8月22日预订了房间1,则用户2无法在同一日期预订房间1。由于我使用数据库,我想我可以通过查询来解决它。特别是我的数据库:

Table Name: reservation

id_book,login,email,typeroom,numroom,arrivaldate,departuredate.

我尝试使用此查询:

SELECT res1.id_prenotazione, res1.typeroom, res1.arrivaldate, res1.departuredate
FROM reservation res1, reservation res2
WHERE ( res1.typeroom = res2.typeroom ) AND (res1.arrivaldate = res2.arrivaldate )

但我没有解决这个问题。你能帮我吗?

更新。

try {

Class.forName("com.mysql.cj.jdbc.Driver");
// out.println("driver loaded");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Hotel?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root" ,"123456789");
out.println("Connect");
Statement st = con.createStatement();
Statement stmt = con.createStatement();
out.println("connection successfull");

String check = ("SELECT res1.id_prenotazione, res1.typeroom, res1.arrivaldate, res1.departuredate\n" +
"FROM reservation res1, reservation res2\n" +
"WHERE res1.numroom = res2.numroom\n" +
"AND ((res1.arrivaldate <= res2.departuredate AND res1.departuredate >= res2.arrivaldate)\n" +
" OR (res2.arrivaldate <= res1.departuredate AND res2.departuredate >= res1.arrivaldate))");

ResultSet rs2 = stmt.executeQuery(check);
int rs = st.executeUpdate("insert into reservation (login,email,typeroom,numroom,arrivaldate,departuredate)values ('"+login+"','"+email+"','"+typeroom+"','"+numroom+"','"+arrivaldate+"','"+departuredate+"')");

我首先尝试使用您答案的数据库版本。我写了这个,为了检查预订是否可以,我应该插入一个 if Controller 吗?

最佳答案

我认为您的意思是同一房间的预订重叠:

SELECT res1.id_prenotazione, res1.typeroom, res1.arrivaldate, res1.departuredate
FROM reservation res1, reservation res2
WHERE res1.room_no = res2.room_no
AND res1.id_prenotazione != res2.id_prenotazione
AND res1.arrivaldate <= res2.departuredate AND res1.departuredata >= res2.arrivaldate

也许还应该检查的是:您使用了日期相等性。如果失败,可能您的表列不是 DATE,而是 DATETIME/TIMESTAMP。这样就可以理解了因为有人可能会在 9:00 离开房间,而下一个人可能会在 12:00 进入房间。

关于mysql - 管理预订系统中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51963946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com