2017-08-27 40 views
0

我想知道有多少用戶來自該城市內的城市和自治市鎮/鄰里。所以我正在尋找一個將輸出這兩個計數結果的查詢。我有一張帶有用戶數據的表格,沒有特定的城市和自治市鎮/社區,但是有來自每個用戶的郵政編碼。這可以用來匹配表格郵編,女巫包含城市和鄰里數據。計數總數和細分

用戶

+----+--------------------+--------------+ 
| ID | User    | Postcode  | 
+----+--------------------+--------------+ 
| 10 | John Doe   | 1100—99-AB | 
| 11 | Shara Lee   | 1201—34-CD | 
| 12 | Patrick Star  | 1100—99-AB | 
| 13 | Oswald Harvey  | 1100—99-AB | 
| 14 | Samuel Jackson  | 1401—34-TR | 
| 15 | Richard Lionheart | 1744—39-AA | 
| 16 | Shamanta Jones  | 2334—95-AC | 
| 17 | James Rooney  | 1401—34-TR | 
| 18 | Chandler Bing  | 3334—23-AA | 
| 19 | Jessica Burner  | 2277—99-RA | 
+----+--------------------+--------------+ 

郵政編碼

+------------+--------------+-------------+ 
| Postcode | City   | Borough  | 
+------------+--------------+-------------+ 
| 1100—99-AB | New York  | Manhattan | 
| 1201—34-CD | New York  | Manhattan | 
| 1401—34-TR | New York  | Bronx  | 
| 1744—39-AA | New York  | Harlem  | 
| 2334—95-AC | Newark  |    | 
| 6334—95-AC | Detroit  | Greektown | 
| 3334—23-AA | Philadelphia | Penn Center | 
| 2277—99-RA | Newark  |    | 
+------------+--------------+-------------+ 

結果我之後

+--------------+--------------+-------------+---------------+ 
| City   | Total_City | Borough  | Total_Borough | 
+--------------+--------------+-------------+---------------+ 
| New York  | 7   | Manhattan | 4    | 
| New York  | 7   | Bronx  | 2    | 
| New York  | 7   | Harlem  | 1    | 
| Newark  | 2   |    | 2    | 
| Philadelphia | 1   | Penn Center | 1    | 
+--------------+--------------+-------------+---------------+ 

這是多遠我跟我的查詢得到。這確實可以計算出自治區/鄰區的數量,但不幸的是,它並未顯示城市總數。

SELECT City, Borough, COUNT(City) AS Total_City, COUNT(Borough) AS Total_Borough, 
    FROM `users` u 
    LEFT JOIN `postcodes` p ON p.postcode = u.postcode 
GROUP BY City, Borough 

見我的例子上http://rextester.com/DFRV4183

+0

差不多,但請參閱[爲什麼我應該爲我認爲是非常簡單的SQL查詢提供一個MCVE](http://meta.stackoverflow.com/questions/333952/why-should-i-provide- an-mcve-for-what-seems-to-very-simple-sql-query) – Strawberry

+0

@Strawberry,你說得很對。我已經從Rextester添加了一個工作示例。 – Kaspar

回答

0

這裏有一種方法......

SELECT a.*, b.total total_city 
    FROM 
    (SELECT p.city 
      , p.borough 
      , COUNT(*) total 
     FROM postcodes p 
     JOIN myusers u 
      ON u.postcode = p.postcode 
     GROUP 
      BY city 
      , borough 
    ) a 
    JOIN 
    (SELECT p.city 
      , COUNT(*) total 
     FROM postcodes p 
     JOIN myusers u 
     ON u.postcode = p.postcode 
    GROUP 
      BY city 
    ) b 
    ON b.city = a.city; 

你也可以做這樣的事情,但我不相信,結果是更容易理解...

SELECT city 
    , borough 
    , COUNT(*) total 
    FROM postcodes p 
    JOIN myusers u 
    ON u.postcode = p.postcode 
GROUP 
    BY city 
    , borough WITH ROLLUP; 

最後,你可以使用變量。也許我會在後面發表一個答案......如果有人不打我的話。

0
SELECT b.City, Borough, Total_City, Total_Borough from 
    (SELECT City, coalesce(COUNT(User),0) AS Total_City 
    FROM `Users` u 
    right JOIN `Postcodes` p ON p.postcode = u.postcode 
    GROUP BY City) as c 
inner join 
(
SELECT City, coalesce(Borough,'Undefined')as Borough,coalesce(COUNT(User),0) AS Total_Borough, p.postcode 
    FROM `users` u 
    right JOIN `postcodes` p ON p.postcode = u.postcode 
GROUP BY City, Borough 
) as b 
on b.City=c.City 
+0

外部查詢有什麼作用? – Strawberry

+0

Thnx,我想使用subquerys是要走的路。你的建議看起來很接近,但不幸的是它沒有列出所有的自治市鎮。 http://sqlfiddle.com/#!9/3e7c6e/12 – Kaspar

+0

立即嘗試並告訴我。 –