简体   繁体   中英

SQL - Combining two queries

In SQL I have wrote these two queries:

Query 1:

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

Query 2:

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

I would ideally like to combine these two queries together, so that the results generated are based on either of these two.

How would i go about doing this? I thought union could be used but not sure if that is the right method to use.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM