提问者:小点点

表/MySQL中不同列的计数和


我试图根据SQL表中特定列的多个不同计数执行求和。 到目前为止的问题是,当我认为synthax会好的时候,我无法执行这个和。 但似乎没有,因为我经常从这个问题中得到问题。

提前感谢您对此情况的任何提示/帮助!

 select SUM(`s1`.`t1`) from (
select
COUNT(DISTINCT s1_global) from NPS_deploiement_synthese as s1 where hubspot_company_id = 2436352252
union
select
COUNT(DISTINCT s2_global) from NPS_deploiement_synthese as s1 where hubspot_company_id = 2436352252
) as t1;

共2个答案

匿名用户

您根本不需要子查询:

select COUNT(DISTINCT s1_global) + COUNT(DISTINCT s2_global)
from NPS_deploiement_synthese s
where hubspot_company_id = 2436352252;

您的版本可以正常工作,但是子查询需要列别名。

匿名用户

您可以添加2 count():

select
  COUNT(DISTINCT s1_global) + COUNT(DISTINCT s2_global) 
from NPS_deploiement_synthese as s1 
where hubspot_company_id = 2436352252