如何在Mysql中计算2 Count(*)的和

时间:2016-02-05 16:01:22

标签: mysql sql

我有两个表,我计算它们的行数。

SELECT COUNT(*) 
FROM docgrados_directores 
WHERE docgrados_directoresleido = '0' AND docgrados_directoresusu = '11' 

result 1

SELECT COUNT(*) 
FROM docgrados_lectores 
WHERE docgrados_lectoresleido = '0' AND docgrados_lectoresusu = '11'

result 1

我需要总数(结果是2)。如何用单个语句对结果求和?什么是正确的语法??

1 个答案:

答案 0 :(得分:2)

使用另一个SELECT添加查询返回的标量值:

SELECT (SELECT COUNT(*) 
        FROM docgrados_directores 
        WHERE docgrados_directoresleido = '0' AND docgrados_directoresusu = '11' ) 
       +    
      (SELECT COUNT(*) 
       FROM docgrados_lectores 
       WHERE docgrados_lectoresleido = '0' AND docgrados_lectoresusu = '11')

如果两个子查询的结果都是2,则上述语句应返回1