如何计算SQL中联合操作的记录数?

时间:2013-05-15 10:50:38

标签: sql count union

我正在使用以下SQL查询

SELECT count(column1) from table1 where CONDITION1
UNION
SELECT count(column1) from table1 where CONDITION2;

当我运行查询时,它会在结果中给出两个数字。但我需要一个数字结果,它将计算上述操作中的记录数。我不知道如何编写该查询。有人能帮助我吗?

2 个答案:

答案 0 :(得分:4)

请尝试:

SELECT count(column1) from table1 where CONDITION1 OR CONDITION2

答案 1 :(得分:3)

这取决于您是否打算table1两次

SELECT
    SUM(TheCount)
FROM
    (
    SELECT count(column1) AS TheCount from table1 where CONDITION1
    UNION
    SELECT count(column1) from table1 where CONDITION2
    ) X;

    SELECT count(column1) AS TheCount
    from table1
    where (CONDITION1) OR (CONDITION2);