SQL Access从两个表中选择NOT IN

时间:2013-10-21 12:09:03

标签: java sql jdbc ms-access-2007

您好我是使用SQL语句的新手,我的SQL语句没有返回正确的结果。 我有两张桌子房间(包含房间详情)和预订(包含预订房间的ID)。 应检索预订的RoomID,其不在到达日期和出发日期范围内。有RoomType =单。最后,从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()

谢谢

3 个答案:

答案 0 :(得分:0)

如果您没有考虑 departureDate ,则会出现错误 试试这个::

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 = ?

答案 1 :(得分:0)

您必须更改以下代码才能从两个表中选择值,并且您正在处理预订系统,该系统需要 date&amp;时间所以我建议您使用时间戳作为 BookArrivalDate和BookDepartureDate 的数据类型。我发布了一个sql查询,它将检查给定的 FromDate&amp;在任何预订日期之间存在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 数据库。 从时间&amp; totime 是您在预准备的报表/日期范围中提供的值。

答案 2 :(得分: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逻辑正确并且能够隔离冲突的预订(以及因此我可以使用的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();