每个不同条件的一个SQL语句中有2个COUNT(MYSQL)

时间:2018-01-31 12:42:10

标签: php mysql sql count multiple-conditions

我一直在寻找一种方法来做到这一点,但是给出的帮助总是有问题。 (它不被MYSQL接受,我的条件比询问问题的人还多。)

我希望能为员工计算两件事。我有3种不同的条件。怎么可能这样做?

下面的COUNT代码是错误的,但它是为了让我知道我想要它做什么。我输出2行所以我需要它们有不同的名称。

仅供参考:(它适用于一个COUNT)

谢谢

SELECT employees.employees_ID,
       employees.name,
       employees.country_count_ID,
       employees.department_ID, 
       employees.initials,
       country.country_initials,

COUNT(distinct clients.retailer_ID when clients.progress_ID = 6) as aRows,
COUNT(distinct case when clients.progress_ID = 7) as bRows

FROM employees

LEFT OUTER JOIN clients ON employees.employees_ID = clients.sales_employees_ID
LEFT OUTER JOIN country ON employees.country_count_ID = country.count_ID

WHERE employees.department_ID = 1

GROUP BY employees.employees_ID,
         employees.name,
         employees.country_count_ID,
         employees.department_ID,
         employees.initials,
         country.country_initials

1 个答案:

答案 0 :(得分:0)

您可以将casecount(distinct)

一起使用
COUNT(distinct case when clients.progress_ID = 6 then clients.retailer_ID  end) as aRows,
COUNT(distinct case when clients.progress_ID = 7 then clients.retailer_ID  end) as bRows,