Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 客户数量超过平均水平的城市。子查询_Mysql_Sql_Subquery_Inner Join_Having Clause - Fatal编程技术网

Mysql 客户数量超过平均水平的城市。子查询

Mysql 客户数量超过平均水平的城市。子查询,mysql,sql,subquery,inner-join,having-clause,Mysql,Sql,Subquery,Inner Join,Having Clause,我有三张桌子 国家/地区表格:id int,国家/地区名称字符串 城市表:id int,城市名称字符串,邮政编码int,国家id int customer\u表:id int、customer\u name字符串、city\u id、customer\u address字符串 我正在寻找一个答案,它将返回所有城市的客户数超过所有城市的平均客户数。对于每个这样的城市,返回国家名称、城市名称和客户数量 输出应该是 country_name, city_name, count 我试图使用子查询,但出

我有三张桌子

国家/地区表格:id int,国家/地区名称字符串 城市表:id int,城市名称字符串,邮政编码int,国家id int customer\u表:id int、customer\u name字符串、city\u id、customer\u address字符串 我正在寻找一个答案,它将返回所有城市的客户数超过所有城市的平均客户数。对于每个这样的城市,返回国家名称、城市名称和客户数量

输出应该是

country_name, city_name, count
我试图使用子查询,但出现了一个错误

选择国家/地区名称、城市名称、计数客户名称 来自农村 city.country\u id=country.id上的内部连接城市 customer.city\u id=city.id上的内部加入客户 其中customer\u name>选择avgcustomer\u name 来自客户 在customer.city\u id=city.id上内部加入customer按id分组 分组1,2 非常感谢您的帮助

Select country.country_name, city.city_name, count(customer.customer_name)
from country
inner join city on city.country_id = country.id
inner join customer on customer.city_id = city.id
group by country.country_name, city.city_name
having count(customer.customer_name) > 
(
  Select count(customer.customer_name) / count(city.city_name) as avg_per_city
  from city
  inner join customer on customer.city_id = city.id
) 
子查询可以更短:

Select count(*) / count(distinct city_id) from customer

查询的连接逻辑看起来不错,但子查询需要修复。我将这样写:

Select co.country_name, ci.city_name, count(*) no_customers
from city ci
inner join country co on co.id = ci. country_id
inner join customer cu on cu.city_id = ci.id
group by co.country_id, co.country_name, ci.city_id, ci.city_name
having count(*) > (
    select count(*) / count(distinct cu1.city_id) from customer cu1
)
注:

不需要在子查询中引入city表;您只能从表customer中获取所需的信息

我在GROUPBY子句中添加了主键列,以便处理现实生活中可能具有相同名称的城市/国家,这种情况确实会发生

表别名使查询更易于编写和读取

我没有一次连接所有三个表,而是使用CTE连接城市和客户表,并获取相关信息,即国家id、城市名称和客户编号。 在这之后,剩下要做的就是从每个对应的国家id中获取国家名称,并选择没有大于平均值的客户的记录。 为了实现这一点,我将CTE与country表连接起来,以获得country_名称,然后应用where子句来过滤所需的结果。
您使用的是哪种数据库管理系统?您已经标记了许多。请仅标记单个RDBMS。我正在使用的MySqlCOUNT不应用于可为NULL的列,除非是有意的,因为当值为NULL时,该记录不被计数。OP没有提供客户名称或城市名称是否可以为空。我同意这不太可能。但是作为一种通用的最佳实践,如果目的是统计记录的数量,那么最好使用count*以避免陷入这种错误。细节:我很清楚。这在我的查询中是个什么问题?您的查询使用的是countspecific列,而不是count*。如果用于计数的列为NULL-able&其中包含实际的NULL,那么计数将不会产生预期的结果,即在本例中返回记录数。我们不知道这个应用程序是关于什么的,所以这是一种可能性,因为OP没有指定这些列是否为NULL。实际上,它是为阅读它的人准备的,而不是批评你的查询。在我的这个查询中,例如city_name如果为空,为什么要算作1?在这个查询中不计算null是绝对正确的!因为有客户被分配到那个城市。您可以通过countcustomer.customer\u name统计该空城市中的客户,但该城市不会被countcity.city\u name统计,因为它是空的。这将使平均值出现偏差。
with table1 as
  (
    select country_id, city_name, count(city_name) no_of_customers
    from city inner join customer
    on city.id=customer.city_id
    group by city_name, country_id
  )
select country_name, city_name, no_of_customers
from table1 inner join country
where no_of_customers >
(
    select count(*)/count(distinct city_id) from customer
)
order by country_name;