2015-02-11 100 views
-1
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個查詢?如何使用子查詢將這些查詢合併爲1

+0

我假設你想在兩行查詢結果?只是INNER JOIN在'RoomID' – LittleBobbyTables 2015-02-11 13:50:10

回答

0

這會給你的所有列兩張桌子在一張桌子裏。

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

Argh,不要在'WHERE'子句中使用impliciit連接,使用顯式連接。該語法顯着過時 – LittleBobbyTables 2015-02-11 13:48:30

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

我不會失望,但這是一個巨大的(和有缺陷的)假設,他們的表結構是相同的;否則,聯盟將失敗。 – LittleBobbyTables 2015-02-11 13:49:26

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)