加入新表时出现的数据不正确

时间:2014-09-21 06:00:59

标签: sql oracle

我不明白这里发生了什么,我有一些代码可以按预期完美运行。但是,一旦我用另一个表修改它,我开始得到不正确的额外数据。

SELECT g.Name
     , b.roomNo 
     , b.From 
     , b.To 
     FROM Booking b
     JOIN Guest g ON g.No = b.gNo
     WHERE b.hNo = 1 AND b.From <= '10-01'
         AND (b.To IS NULL OR b.To >= '10-01'); 

我明白了,所有这些信息都是正确的,我已经手动验证了它。

NAME      ROOMNO FROM  TOO
--------- ------ ----- -----                                
Bob            5 09-27 10-04                                
Joe            4 09-21 10-24                                
Frank          7 09-30      
Peter          3 09-22 10-02

我将其修改为:

SELECT g.Name
     , b.roomNo
     , r.price   --<-- new
     , b.From
     , b.To 
     FROM Booking b
     JOIN Guest g ON g.No = b.gNo
     JOIN Room r ON r.roomNo = b.roomNo   --<-- new
     WHERE b.hNo = 1 AND b.From <= '10-01'
         AND (b.To IS NULL OR b.To >= '10-01'); 

这就是我回来的......现在我已经检查了一下,我知道每个房间只有一个价格,但我会收回每个房价的两个价格。我无法理解这一点。

NAME      PRICE ROOMNO FROM  TOO
--------- ----- ------ ----- -----                                
Bob          55      5 09-27 10-04 -- <--correct price
Bob          60      5 09-27 10-04                                
Joe          55      4 09-21 10-24 -- <--correct price
Joe          60      4 09-21 10-24                                
Frank        90      7 09-30       -- <--correct price
Frank       100      7 09-30                                           
Peter        55      3 09-22 10-02 -- <--correct price
Peter        60      3 09-22 10-02

这也可能有帮助......

SELECT b.roomNo
 FROM Booking b
 WHERE b.hNo = 1;

给出正确的10个结果:

ROOMNO                                                                      
------                                                                      
     7                                                                      
     5                                                                      
     5                                                                      
     5                                                                      
     1                                                                      
     3                                                                      
     4                                                                      
     7                                                                      
     2                                                                      
     3  

然而....

SELECT b.roomNo
    FROM Booking b
    JOIN Room r ON b.roomNo = r.roomNo
    WHERE b.hNo = 1;

给出80个结果......

1 个答案:

答案 0 :(得分:1)

在我不想知道多少小时之后,确定找到了我自己的答案,这很简单。

JOIN Room r ON r.roomNo = b.roomNo AND b.hotelNo = r.hotelNo

这只是对代码的一点点添加,因为它不知道hotelNo字段也应该加入。

SELECT g.Name
 , b.roomNo
 , r.price
 , b.From
 , b.To 
 FROM Booking b
 JOIN Guest g ON g.No = b.gNo
 JOIN Room r ON r.roomNo = b.roomNo AND b.hotelNo = r.hotelNo  --<-- new
 WHERE b.hNo = 1 AND b.From <= '10-01'
     AND (b.To IS NULL OR b.To >= '10-01');