【发布时间】:2020-03-12 20:38:45
【问题描述】:
在Mysql中:
我在表格中有客户姓名(主键)、城市和金额:
表格城市:
customer location amount
Cust1 New York, USA 200
Cust2 New York, USA 300
Cust3 Chicago, USA 100
Cust4 Paris, France 400
Cust5 Nice, France 500
Cust6 Milan, Italy 600
Cust7 Mumbai, India 0
此表中位置名称的格式为:
<city>, <country>
同:
<city><comma><space><country>
表格国家(主键):
Name
USA
France
Italy
India
Thailand
我想知道每个国家有多少个城市,以及每个国家的平均数量。喜欢:
Country Count Average
USA 3 200 // (200 + 300 + 100) / 3
France 2 450 // (400 + 500) / 2
Italy 1 600 // (600) / 1
India 1 0 // (0) / 1
Thailand 0 0 // 0
所以,我的查询是:
SELECT t1.name Country, count(distinct t2.location) Count
FROM Country t1 LEFT JOIN Cities t2
ON t2.location LIKE concat('%, ', t1.name)
GROUP BY t1.name ORDER BY Count DESC
但它不给出平均数据,它只给出国家名称和计数
【问题讨论】:
-
客户名称不是可持续的主键
-
你好,只是为了代表,我想说城市可以重复,我想统计一下全国的平均值。
标签: mysql sql join group-by count