用于检索不在外键表中的记录的Java Query

时间:2013-07-26 04:49:43

标签: java database

您好我想要一个查询来检索不在另一个表中的记录。例如(列出所有在新加坡酒店无人居住的房间号码)。

Room               Hotel            Booking
--------      --------------   ------------------
room_no          hotel_no           hotel_no
hotel_no         hotel_name         room_no
                                    date_from
                                    date_to

有没有办法只用一个SQL查询来检索这条记录?

2 个答案:

答案 0 :(得分:1)

您可以尝试此查询

 select room_no
   from room
  where room_no not in 
        (select room_no 
           from Booking 
          where hotel_no = singapore_hotel_no)

答案 1 :(得分:0)

Select room.roon_num
from Room left outer join Hotel on room.room_no = hotel.room_no
left outer join Booking on room.room_no = booking.room_no and hotel.hotel_no = booking.hotel_no
where hotel.hotel_name = ? 
and not (? between (booking.date_from , booking.date_to))

首先?'singapore hotel' 第二个?是您要查询的日期