Mysql 在查询中使用having子句两次

Mysql 在查询中使用having子句两次,mysql,sql,query-optimization,having,Mysql,Sql,Query Optimization,Having,我有两个问题 select a,b,c,count(d) as first_count from test group by a,b,c having (count(c)>1) 第二个呢 select a,b,c,count(d) as second_count from test group by a,b,c having (count(c)>100) 如何在一个查询中执行上述操作,以便在结果中有以下列:a、b、c、count(d)作为第一个计

我有两个问题

select a,b,c,count(d) as first_count from
test
group by a,b,c
having (count(c)>1)
第二个呢

    select a,b,c,count(d) as second_count  from
    test
    group by a,b,c
    having (count(c)>100)

如何在一个查询中执行上述操作,以便在结果中有以下列:
a、b、c、count(d)作为第一个计数,count(d)作为第二个计数

也许这会对您有所帮助

 select a,b,c,(select count(d)  from test having (count(c)>1) )   as first_count,
              (select count(d)  from test having (count(c)>100) ) as second_count
 FROM test
 group by a,b,c

如果要合并两个查询结果,可以使用
UNION

(select a,b,c,count(d) as first_count ,0 as second_count
from test
group by a,b,c
having first_count>1)
UNION
(select a,b,c,0 as first_count,count(d) as second_count  
from test
group by a,b,c
having second_count>100)
或者,在单个查询中,您可以使用
操作或
操作根据您的条件使用两种计数结果进行筛选

select a,b,c,count(d) as first_count from,count(d) as second_count
FROM test
group by a,b,c
having first_count>1 AND second_count >100
这样试试看

SELECT a, b, c, 
       SUM(count >   1) first_count,
       SUM(count > 100) second_count 
  FROM 
(
  SELECT a, b, c, COUNT(*) count
    FROM test
   GROUP BY a, b, c
  HAVING COUNT(*) > 1
) q
 GROUP BY a, b, c

如果在选择列表中使用标量子查询,则不需要group by。
FROM
位于表名之前。它在这两个表中提供相同的值counts@Developer从查询中添加所需的样本数据和所需结果集请显示所需结果的小样本。@开发人员有何帮助?