【发布时间】:2020-12-05 12:49:10
【问题描述】:
我有三张桌子
-
country_table:id int, country_name string -
city_table:id int, city_name string, postal_code int, country_id int -
customer_table:id int, customer_name string, city_id id, customer_address string
我正在寻找一个答案,它将返回所有城市的客户数量超过所有城市的平均客户数量。对于每个这样的城市,返回国家名称、城市名称、客户数量。
输出应该是
country_name, city_name, count
我尝试使用子查询但出现错误
Select country_name, city_name, count(customer_name)
from country
inner join city on city.country_id = country.id
inner join customer on customer.city_id = city.id
where customer_name > (select avg(customer_name)
from customer
inner join customer on customer.city_id = city.id group by id)
group by 1, 2
非常感谢任何帮助
【问题讨论】:
-
您使用的是哪个 DBMS?你已经标记了很多。
-
请只标记一个 RDBMS。
-
我正在使用 MySql
标签: mysql sql subquery inner-join having-clause