1

您好,我对使用 SQL 语句很陌生,我的 SQL 语句没有返回正确的结果。我有两张桌子房间(包含房间详细信息)和预订(包含预订房间的 ID)。应检索 Booking 的 RoomID,它不在 Arrival Date AND Departure Date 的范围内,并且 RoomType = single。最后从 Room 表中搜索 ID 以显示详细信息。

psmt = con.prepareStatement("SELECT RmID,RmName,RmType,RmDescription,RmRate,RmMaxPerson FROM Room WHERE RmID NOT IN (SELECT RmID FROM Booking WHERE ((BookArrivalDate >= ? AND BookArrivalDate < ?))) AND RmType = ?");
psmt.setDate(1, sqlDateAStart);
psmt.setDate(2, sqlDateAEnd);
psmt.setString(3, RoomType);
rs = psmt.executeQuery()

谢谢

4

3 回答 3

0

在这种情况下,隔离应用程序的特定部分并使用小测试程序对其进行试验会非常有帮助。例如,我在 Access 中创建了一个 [Booking] 表...

BookingID  RmID  BookArrivalDate  BookDepartureDate
---------  ----  ---------------  -----------------
        1   101  2013-10-21       2013-10-23       
        2   102  2013-11-01       2013-11-03       

...然后使用以下小测试程序对其运行查询:

import java.sql.*;
import org.joda.time.*;

public class JDBCQuery {
    public static void main(String args[]) {
        Connection conn = null;
        PreparedStatement s = null;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection(
                    "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};" + 
                    "DBQ=C:\\Users\\Public\\Database1.accdb;");

            LocalDate requestedArrivalDate = LocalDate.parse("2013-10-12");
            LocalDate requestedDepartureDate = LocalDate.parse("2013-10-21");

            s = conn.prepareStatement(
                    "SELECT BookingID, RmID FROM Booking WHERE BookDepartureDate > ? AND BookArrivalDate < ?");
            s.setDate(1, new java.sql.Date(requestedArrivalDate.toDate().getTime()));
            s.setDate(2, new java.sql.Date(requestedDepartureDate.toDate().getTime()));
            ResultSet rs = s.executeQuery();

            System.out.println(String.format("Requested arrival: %s", requestedArrivalDate.toString()));
            System.out.println(String.format("Requested departure: %s", requestedDepartureDate.toString()));
            System.out.println("The following bookings conflict with that request...");
            if (rs!=null) {
                while (rs.next()) {
                    System.out.println(String.format("BookingID: %d, RmId: %d", rs.getInt("BookingID"), rs.getInt("RmID")) );
                }
            }
            System.out.println("(end of list)");

        } catch( Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                if (s != null) {
                    s.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch( Exception e ) {
                e.printStackTrace();
            }
        }
    }
}

一旦我确信我的 SQL 逻辑是正确的并且能够隔离冲突的 Bookings(以及因此我无法使用的 RmID 值),那么很明显我希望我的最终查询看起来像这样:

SQL = 
        "SELECT RmID FROM Room " +
        "WHERE " + 
            "RmID NOT IN " +
                "(" +
                    "SELECT RmID FROM Booking " +
                    "WHERE BookDepartureDate > ? AND BookArrivalDate < ? " +
                ")" +
            "AND RmType = ?";
s = conn.prepareStatement(SQL);
s.setDate(1, new java.sql.Date(requestedArrivalDate.toDate().getTime()));
s.setDate(2, new java.sql.Date(requestedDepartureDate.toDate().getTime()));
s.setString(3, "Single");
ResultSet rs = s.executeQuery();
于 2013-10-21T17:18:42.670 回答
0

你有一个错误,你没有考虑出发日期 试试这个::

SELECT 
RmID,
RmName,
RmType,
RmDescription,
RmRate,RmMaxPerson 
FROM 
Room r
INNER JOIN Booking b on (r.RmID =b.RmID ) 
WHERE BookArrivalDate < ? AND BookDepartureDate > ? AND RmType = ?
于 2013-10-21T12:11:24.443 回答
0

您必须更改以下代码以从两个表中选择值,并且您正在处理需要日期和时间的预订系统,因此我建议您使用Timestamp作为BookArrivalDate 和 BookDepartureDate的数据类型。我发布了一个 sql 查询,它将检查给定的FromDate 和 ToDate是否存在于任何预定日期之间,如果有则返回结果。

SELECT RmID,RmName,RmType,RmDescription,RmRate,RmMaxPerson
FROM Room,Booking
WHERE 
(RmType ='single') 
AND 
((
('fromtime'>=arrivaltime)
AND 
('totime'<=departuretime))
OR 
(
('fromtime'<=arrivaltime)
 AND 
 ('totime'>=departuretime))
OR 
(
('fromtime'<=arrivaltime) 
AND 
('totime'>=arrivaltime)) 
OR 
(
('totime'>=arrivaltime)
 AND
  ('totime'<=departuretime)) 
OR 
(
('totime'>=departuretime)
 AND
  ('fromtime'<=departuretime))
OR 
(
('fromtime'>=arrivaltime)
 AND
  ('fromtime'<=departuretime)
)) 

这里的到达时间是您的BookArrivalDate,它存在于数据库中,而离开时间您的BookDepartureDate,它存在于数据库中。fromtime 和 totime是您在准备好的声明/日期范围中给出的值。

于 2013-10-21T12:46:02.900 回答