简体   繁体   中英

Microsoft Access SQL Query - get rid of certain results

I want to find lecture rooms (their room code) which are of capacity 100 or more and are free (ie not booked) on Tuesday at 12pm.

In the room table, there are rCode & capacity. In the roomBooking table, there are dayReq, timeReq & roomCode.

The SQL Query I have so far is

SELECT rCode, capacity 
FROM room
INNER JOIN roomBooking
ON room.rCode = roomBooking.roomCode
WHERE capacity >= 100;

I want to get rid of the results that involve dayReq = "Tuesday" AND timeReq = "12:00:00" but I don't know the correct operator to do so.

To get what you want, you are going to need a left join (or a subquery in the where clause). The idea is to find get all rooms along with any possible matches the room has to the booking criteria. You then want the rooms where there is no match:

SELECT room.rCode, room.capacity 
FROM room LEFT JOIN
     roomBooking
     ON room.rCode = roomBooking.roomCode AND
        dayReq = 'Tuesday' AND timeReq = '12:00:00'
WHERE room.capacity >= 100 AND
      roomBooking.roomCode IS NULL;

MS Access is really weird about many things, including joins with multiple keys. Just do this with a correlated subquery:

SELECT room.rCode, room.capacity 
FROM room
WHERE NOT EXISTS (SELECT 1
                  FROM roomBooking
                  WHERE room.rCode = roomBooking.roomCode AND
                        dayReq = "Tuesday" AND timeReq = "12:00:00"
                 ) AND
      room.capacity >= 100;

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