0

我正在尝试返回ts_allocation表中不存在的所有房间(即尚未分配)或ts_allocation表中的 Pending、Failed 或 Declined 状态。房间可以满足其中一项条款。

但是D.0.02等几个值被省略了(请参考fiddle)?为什么是这样?它以上述状态存在于ts_allocation中 - 但它似乎不满足某个条款。我该如何解决这个问题?

这是我的 SQLFiddle:http ://sqlfiddle.com/#!2/e1021/6

SELECT 
  *
FROM 
  ts_room rm
WHERE 
  NOT EXISTS (
    SELECT 1
    FROM ts_roompref rp
      JOIN ts_request rq
      ON rp.request_id = rq.id
      AND day_id = 1
      AND period_id = 1
    WHERE 
      rm.id = rp.room_id)
AND NOT EXISTS (
    SELECT 1
    FROM ts_roompref rp
      JOIN ts_allocation a
      ON rp.request_id = a.request_id
            AND a.status <> 'Pending'
            AND a.status <> 'Failed'
            AND a.status <> 'Declined'
    WHERE 
      rm.id = rp.room_id)
4

1 回答 1

0
SELECT  rm.* 
FROM    ts_room rm
        LEFT JOIN (
            SELECT  a.*, rp.room_id 
            FROM    ts_roompref rp 
                    INNER JOIN  dbo.ts_allocation a on a.request_id = rp.request_id) x 
                    ON  x.room_id = rm.id
WHERE   x.id is null    
        or (x.id is not null and x.status in ('Pending', 'Failed','Declined'))

x.id is null为您提供所有尚未分配的房间,并且该or部分为您提供更多结果- 那些具有待处理、拒绝或失败状态的房间。

于 2013-02-19T02:32:52.693 回答