我正在尝试聚合结果表中的两列,但我无法理解它...我有一个星型模式和以下查询:
select 1und1.SubCategory,count(industry_facts.NACE_ID), statistics_2016.businesses
from industry_facts, 1und1, statistics_2016
where 1und1.NACE_ID = industry_facts.NACE_ID
and statistics_2016.Stat_ID = industry_facts.Stat_ID
group by 1und1.SubCategory ASC, statistics_2016.businesses
结果表以这种方式返回正确的结果:
这是架构:
我无法找到按子类别聚合它的方法...所以它看起来像住宿,660, 1004390
尝试在 group by 子句的和处进行汇总,但这只聚合第二列,第三列总是返回“NULL”
你有什么想法吗?非常感谢您的帮助!
最佳答案
从GROUP BY
中删除business
。另外,决不在FROM
子句中使用逗号。 始终使用正确、明确的JOIN
语法:
select u.SubCategory, count(i.NACE_ID)
from industry_facts join i
1und1 u
on u.NACE_ID = i.NACE_ID join
statistics_2016 s
on s.Stat_ID = i.Stat_ID
group by u.SubCategory ASC;
如果您想要这些商家,则可以在 SELECT
或 group_concat(distinct s.businesses)
中使用 group_concat(s.businesses)
>.
关于mysql - 结果中 2 列的聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43706381/