我在弄清楚如何从结果中删除具有唯一ID的行时遇到了一些麻烦。
基本上我正在运行此查询:
SELECT
date(created) AS created, COUNT(customer_id) AS cnt, customer_id
FROM
case
WHERE
customer_id IN (16533 , 7186, 17727, 2442, 2646)
GROUP BY DATE(`created`)
这将为我提供一个客户每天创建案例的频率列表:
Date cnt customer_id
2017-06-26 9 17727
2017-06-27 1 17727
2017-05-10 16 16533
2016-06-13 1 7186
2016-10-20 1 7186
2016-10-21 1 7186
2016-10-28 1 7186
...
customer_id 16533只在列表中出现一次,我想将其从结果中删除?
亲切的问候
答案 0 :(得分:2)
有点像这样:
SELECT
date(created) AS created, COUNT(customer_id) AS cnt, customer_id
FROM
case
WHERE
customer_id IN (16533 , 7186, 17727, 2442, 2646)
AND customer_id not in(
Select customer_id from case
group by customer_id
having count(*)=1)
GROUP BY DATE(`created`)
答案 1 :(得分:0)
我有新闻给你。这个查询没有意义:
SELECT date(created) AS created, COUNT(customer_id) AS cnt, customer_id
FROM `case`
WHERE customer_id IN (16533 , 7186, 17727, 2442, 2646)
GROUP BY DATE(`created`);
customer_id
假设有什么价值?它从该日期返回任意customer_id
。它是不确定的,可能是那个日期的任何一个。
如果您需要列表,请使用group_concat()
:
SELECT date(created) AS created, COUNT(customer_id) AS cnt,
group_concat(customer_id) as customer_ids
FROM `case`
WHERE customer_id IN (16533 , 7186, 17727, 2442, 2646)
GROUP BY DATE(`created`);
如果您想要消除仅出现一次的客户,请明确这样做:
SELECT date(created) AS created, COUNT(customer_id) AS cnt,
group_concat(customer_id) as customer_ids
FROM `case` c
WHERE c.customer_id IN (16533, 7186, 17727, 2442, 2646) and
not exists (select 1
from `case` c2
where c2.customer_id = c.customer_id and
c2.case_id <> c.case_id -- or whatever the primary key is
)
GROUP BY DATE(`created`);
答案 2 :(得分:0)
Select * From (SELECT
date(created) AS created, COUNT(customer_id) AS cnt, customer_id
FROM
case
WHERE
customer_id IN (16533 , 7186, 17727, 2442, 2646)
GROUP BY DATE(`created`)) t
Where t.customer_id NOT IN (16533)
答案 3 :(得分:-1)
如果您要求出现不止一次的客户,请不要在此处使用密钥.....以及按正确的字段进行分组.....
SELECT
date(created) AS created, customer_Id, COUNT(*) AS cnt
FROM
case
WHERE
customer_id IN (16533 , 7186, 17727, 2442, 2646)
GROUP BY DATE(`created`), customer_id
HAVING COUNT(*) >1
如果您只想排除16533,请不要在IN套装中使用它。