我有一个类似的查询:
SELECT COUNT(DISTINCT `ID`) FROM db1.table UNION ALL SELECT COUNT(DISTINCT `ID`) FROM db2.table
我的真实查询更复杂,我有LEFT JOIN和多个条件等...
此查询返回给我一个包含2个结果的数组:Count 1
和Count 2
我怎样才能返回一个结果? Count 1 + Count 2
?
谢谢!
答案 0 :(得分:1)
试试这个:
SELECT (SELECT COUNT(DISTINCT `ID`) FROM db1.table) +
(SELECT COUNT(DISTINCT `ID`) FROM db2.table)
如果您还想单独返回db1
,db2
个计数,请使用此查询:
SELECT countDb1, countDb2, countDb1 + countDb2 AS total
FROM (
SELECT (SELECT COUNT(DISTINCT `ID`) FROM db1.table) AS countDb1,
(SELECT COUNT(DISTINCT `ID`) FROM db2.table) AS countDb2) AS t