SQL计数聚合函数和子查询

时间:2015-09-27 12:33:44

标签: mysql sql aggregate-functions

我只是在聚合函数和子查询上学习SQL

我有一个包含c0, c1, c2, c3列的数据库表。

我的查询:

SELECT ID, count(ID) 
FROM ((select ID from tbl1 where c0 BETWEEN 4 and 7) 
UNION ALL
(select ID from tbl1 where c1 BETWEEN 5 and 7) 
UNION ALL
(select ID from tbl1 where c2 BETWEEN 6 and 10) 
UNION ALL
(select ID from tbl1 where c3 BETWEEN 1 and 5)) AS tbl
GROUP BY ID HAVING count(ID) >= 2

上面的查询可以重写,结果会更快吗?或者如何让我的查询更快?

1 个答案:

答案 0 :(得分:1)

我认为你根本不需要聚合。只计算每一行的匹配。

假设id在表格中是唯一的:

select id,
       ((c0 between 4 and 7) + (c1 between 5 and 7) + (c2 between 6 and 10) +
        (c3 between 1 and 5)
       ) as cnt
from tbl1
having cnt >= 2;

MySQL将数字上下文中的布尔值视为数字,其中1表示true,0表示false,因此表达式c0 between 4 and 7基本上等同于case when c0 between 4 and 7 then 1 else 0 end,但更容易编写。

MySQL还扩展了having子句,因此它可以在没有group by的情况下工作。在这种情况下,它的行为类似于where,但您可以使用select中定义的别名。

注意:如果列的值为NULL,则稍微复杂一些。

如果id不是唯一的,那么你基本上可以用聚合做同样的事情:

select id,
       sum((c0 between 4 and 7) + (c1 between 5 and 7) + (c2 between 6 and 10) +
           (c3 between 1 and 5)
          ) as cnt
from tbl1
group by id
having cnt >= 2;

但是,名为id的列应该是唯一的。