码迷,mamicode.com
首页 > 其他好文 > 详细

[CareerCup] 15.1 Renting Apartment 租房

时间:2016-04-03 08:57:37      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

 

Write a SQL query to get a list of tenants who are renting more than one apartment.

 

-- TABLE Apartments

+-------+------------+------------+
| AptID | UnitNumber | BuildingID |
+-------+------------+------------+
|   101 | A1         |          1 |
|   102 | A2         |          2 |
|   103 | A3         |          3 |
|   201 | B1         |          4 |
|   202 | B2         |          5 |
+-------+------------+------------+

 

-- TABLE Buildings

+------------+-----------+---------------+---------------+
| BuildingID | ComplexID | BuildingName  | Address       |
+------------+-----------+---------------+---------------+
|          1 |        11 | Eastern Hills | San Diego, CA |
|          2 |        12 | East End      | Seattle, WA   |
|          3 |        13 | North Park    | New York      |
|          4 |        14 | South Lake    | Orlando, FL   |
|          5 |        15 | West Forest   | Atlanta, GA   |
+------------+-----------+---------------+---------------+

 

-- TABLE Tenants

+----------+------------+
| TenantID | TenantName |
+----------+------------+
|     1000 | Zhang San  |
|     1001 | Li Si      |
|     1002 | Wang Wu    |
|     1003 | Yang Liu   |
+----------+------------+

 

-- TABLE Complexes

+-----------+---------------+
| ComplexID | ComplexName   |
+-----------+---------------+
|        11 | Luxuary World |
|        12 | Paradise      |
|        13 | Woderland     |
|        14 | Dreamland     |
|        15 | LostParis     |
+-----------+---------------+

 

-- TABLE AptTenants

+----------+-------+
| TenantID | AptID |
+----------+-------+
|     1000 |   102 |
|     1001 |   102 |
|     1002 |   101 |
|     1002 |   103 |
|     1002 |   201 |
|     1003 |   202 |
+----------+-------+

 

-- TABLE Requests

+-----------+---------+-------+-------------+
| RequestID | Status  | AptID | Description |
+-----------+---------+-------+-------------+
|        50 | Rented  |   101 |             |
|        60 | Pending |   103 |             |
+-----------+---------+-------+-------------+

 

这道题让我们租了不止一间公寓的人,那么我们需要两个表Tenants和AptTenants,其他的表都不需要,那么我们可以用Inner Join来关联两个表,关于SQL的各种Join请参见我之前的博客SQL Left Join, Right Join, Inner Join, and Natural Join 各种Join小结,然后我们还需要用Group by和Count关键字来表示在AptTenants表中出现的次数大于1的TenantID,然后在Tenants表中找到名字返回:

 

解法一:

SELECT TenantName FROM Tenants
INNER JOIN
(SELECT TenantID FROM AptTenants
GROUP BY TenantID HAVING COUNT(*) > 1) C
ON Tenants.TenantID = C.TenantID;

 

下面这种解法用了Using关键字指定了相同列TenantID:

 

解法二:

SELECT TenantName FROM Tenants
INNER JOIN
(SELECT TenantID FROM AptTenants
GROUP BY TenantID HAVING COUNT(*) > 1) C
USING (TenantID);

 

CareerCup All in One 题目汇总

[CareerCup] 15.1 Renting Apartment 租房

标签:

原文地址:http://www.cnblogs.com/grandyang/p/5349036.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!