2014-10-07 226 views
0

在SQL我已經寫了這兩個查詢:SQL - 合併兩個查詢

查詢1:

SELECT HotelID, HotelName, Email FROM Hotel 
WHERE HotelID IN (SELECT Branch.HotelID FROM Branch 
WHERE BranchAddress LIKE '%London%'); 

查詢2:

SELECT Branch.HotelID, COUNT(*) 
FROM Branch 
GROUP BY HotelID 
HAVING COUNT(*) > 5; 

我會非常喜歡這兩個查詢組合一起,以便所產生的結果基於這兩者中的任何一個。

我該如何去做這件事?我認爲工會可以使用,但不知道這是否是正確的使用方法。

+0

'select h.HotelID,h.HotelName,h.Email,count(*)from Hotel h join Branch b on h.HotelID = b.BranchID where b.BranchAddress like%LONDON%group by h。 HotelID,h.HotelName,h.Email有(*)> 5'? – 2014-10-07 04:38:06

回答

0
SELECT a.hotelid, 
     a.hotelname, 
     a.email, 
     Count(*) 
FROM hotel a 
     JOIN branch b 
     ON a.hotelid = b.hotelid 
WHERE b.branchaddress LIKE '%London%' 
GROUP BY a.hotelid, 
      a.hotelname, 
      a.email 
HAVING Count(*) > 5