计算总数和细目数

时间:2017-08-27 16:41:28

标签: mysql

我想知道有多少用户来自该城市的哪个城市和自治市镇/社区。所以我正在寻找一个将输出这两个计数结果的查询。我有一个包含用户数据的表,没有特定的城市和自治市/社区,但有每个用户的邮政编码。这可以用来匹配表格的邮政编码,包含城市和社区数据。

用户

+----+--------------------+--------------+
| 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

上的示例

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;

最后,您可以使用变量。也许我会在以后发布这样的答案......如果有人不打扰我的话。