如何使用子查询将这些查询合并为1

时间:2015-02-11 13:29:01

标签: sql subquery correlated-subquery

 Select * from HotelPerson 
 Where RoomID IN (select ID from HotelRoom Where BookingID = 36 )

 Select * from HotelCancelationPolicy 
 Where RoomID IN (select ID from HotelRoom Where BookingID = 36 )

如何将这两个查询合并为1个查询?

3 个答案:

答案 0 :(得分:0)

使用UNION获取两个表中所有行的不同元素或UNION ALL

 Select * from HotelPerson 
 Where RoomID IN (select ID from HotelRoom Where BookingID = 36 )
 UNION ALL
 Select * from HotelCancelationPolicy 
 Where RoomID IN (select ID from HotelRoom Where BookingID = 36 )

答案 1 :(得分:0)

我想你想加入这两个表:

    select * 
      from HotelPerson hp
inner join HotelCancelationPolicy hcp
        on hp.RoomId = hcp.RoomId
     where hp.RoomID IN (select ID 
                           from HotelRoom 
                          where BookingID = 36 )

答案 2 :(得分:0)

这将为您提供一个表中两个表的所有列。

SELECT * 
FROM HotelPerson A, HotelCancelationPolicy B
WHERE A.RoomID = B.RoomID
AND A.RoomID IN (SELECT ID FROM HotelRoom WHERE BookingID = 36)