计数如果MySql与众不同

时间:2018-07-19 07:05:03

标签: mysql sql

请帮助我进行此查询,我有此数据

customeid | param
1001      |  A
1001      |  B
1001      |  C
1002      |  B
1002      |  A
1003      |  A
1003      |  B
1004      |  A

我需要区分自定义,我使用此查询,但结果不正确

SELECT
  COUNT(DISTINCT IF(param not in ('A'),1,0))
FROM
  table

输出结果

count   
  3 (from customeid 1001,1002,1003)

如果参数不在A中并且无法在何处添加查询,如何区分customeid

4 个答案:

答案 0 :(得分:1)

您将countif一起使用distinct来判断没有问题,但是有问题,如果满足条件,则需要返回customeid,否则您需要返回null

您可以遵循此查询。

SELECT
  COUNT(DISTINCT IF(param not in ('A'),customeid,NULL))
FROM
  table

sqlfiddle

注意

COUNT在该值为null时不会累积

答案 1 :(得分:0)

使用case when和聚合函数group by

 select customeid,count(*) as Count
 from (    
    select (case when param!='A' then customeid else end) as customeid    from your_table
) T group by customeid

答案 2 :(得分:0)

select customeid,count(customeid)
from table
where param <>'A'
group by customeid

答案 3 :(得分:0)

SELECT DISTINCT COSTUMEID, COUNT(DISTINCT (PARAM))
FROM TABLE 
WHERE PARAM NOT IN (('A'),1,0) 
GROUP BY COSTUMEID