简体   繁体   English

从表中排除某些值的 SQL 查询

[英]SQL query to exclude certain values from table

在此处输入图片说明

Hi everyone I am in the process of building a room allocating system that checks if a room is available on a given date.大家好,我正在构建一个房间分配系统,用于检查在给定日期是否有房间可用。 There are about 20 unique room ids and I've just included as snippet of several above.大约有 20 个独特的房间 ID,我只是作为上面几个的片段包括在内。

So for example if the user types in the date of 2012-01-01 I would want the room_id of 1 to be excluded from the results because as seen above it has been already booked for that date.因此,例如,如果用户输入 2012-01-01 的日期,我希望从结果中排除 1 的room_id ,因为如上所示,该日期已被预订。

In a sense any match found between the user's in-putted date should cause the entire list of corresponding room_id's to be excluded from the list.从某种意义上说,在用户输入的日期之间找到的任何匹配都会导致相应的 room_id 的整个列表从列表中排除。

I used the below SQL query:我使用了以下 SQL 查询:

SELECT DISTINCT room_id
FROM room_booking
WHERE date_booked<>'$date'

Unfortunately it doesn't work because although it excludes the result for a single row, it includes the rest of the results for a given room_id ?不幸的是它不起作用,因为虽然它排除了单行的结果,但它包括给定room_id的其余结果?

You could use a NOT EXISTS您可以使用NOT EXISTS

SELECT DISTINCT room_id
  FROM room_booking rb1
 WHERE NOT EXISTS( SELECT 1
                     FROM room_booking rb2
                    WHERE rb2.date_booked = <<date>>
                      AND rb2.room_id     = rb1.room_id )

or a NOT INNOT IN

SELECT DISTINCT room_id
  FROM room_booking
 WHERE room_id NOT IN (SELECT room_id
                         FROM room_booking rb2
                        WHERE rb2.date_booked = <<date>>)

Not tried this, but here goes没有试过这个,但这里是

SELECT DISTINCT room_id
FROM room_booking
WHERE room_id NOT IN 
    (SELECT DISTINCT room_id
     FROM room_booking
     WHERE date_booked = '$date'
    )

Considering you have a column called RoomID I'm going to assume that there is a Room table with a corresponding column RoomID - this is probably a much better place to look for all rooms as you can't guarantee every room has a booking, and so can't rely on the booking table alone.考虑到您有一个名为RoomID的列,我将假设有一个带有相应列 RoomID 的 Room 表 - 这可能是查找所有房间的更好的地方,因为您不能保证每个房间都有预订,并且所以不能仅仅依靠预订表。

Then you can look up with a sub-query which rooms are booked on a date, then return all the RoomIds which aren't in that list.然后,您可以使用子查询查找某个日期预订了哪些房间,然后返回所有不在该列表中的 RoomId。

select RoomID from Room
where RoomID not in
(select RoomID from room_booking
where date = @date)

The above should work however may potentially become slow if a lot of rooms are added.以上应该可以工作,但是如果添加了很多房间,可能会变慢。 You may want to consider a left outer join.您可能需要考虑左外连接。 It will typically be faster as the above will be performing sub queries ie For every room, requery the database to check if it has a value of this date which results in 40 * 40 checks each time this query is executed and will increase with the number of current bookings.它通常会更快,因为上面将执行子查询,即对于每个房间,重新查询数据库以检查它是否具有此日期的值,每次执行此查询时都会导致 40 * 40 检查,并且会随着数量增加当前预订。

The below should be more efficient and should be valid on quite a few database platforms.以下应该更有效,并且应该在相当多的数据库平台上有效。 Of course, this depends on how much the count of values may change if there are any string performance related items to consider...当然,这取决于如果有任何与字符串性能相关的项目需要考虑,值的计数可能会改变多少......

SELECT DISTINCT room_id
           FROM room_booking rb1
LEFT OUTER JOIN room_booking datecheck
             ON rb1.room_id = datecheck.room_id
            AND datecheck.date_booked  =  @date
          WHERE datecheck.room_id is null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM