如何获得这三个COUNTS()的SUM?

时间:2011-07-26 22:15:44

标签: mysql count sum

这是我的疑问:

SELECT (SELECT COUNT(`id`) FROM `table1` WHERE `id` = '1') AS `count1`,
(SELECT COUNT(`id`) FROM `table2` WHERE `id` = '1') AS `count2`,
(SELECT COUNT(`id`) FROM `table3` WHERE `id` = '1') AS `count3`

如何获得count1, count2, count3的总和?

我有什么方法可以在上面的查询

1 个答案:

答案 0 :(得分:4)

不确定。只需将值加在一起:

SELECT (SELECT COUNT(`id`) FROM `table1` WHERE `id` = '1') +
       (SELECT COUNT(`id`) FROM `table2` WHERE `id` = '1') +
       (SELECT COUNT(`id`) FROM `table3` WHERE `id` = '1') AS `sum` ...