How to merge these queries into 1 using subquery

こ雲淡風輕ζ 提交于 2019-12-13 09:51:49

问题


 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 )

How can I merge these both queries into 1 query ?


回答1:


This will give you all the columns from both tables in one table.

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



回答2:


Use UNION to Get Distinct elements or UNION ALL for all rows from both tables

 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 )



回答3:


I guess you want to join the two tables:

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


来源:https://stackoverflow.com/questions/28455607/how-to-merge-these-queries-into-1-using-subquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!