我对DB2 SQL服务器很不熟悉。我试图从多个表中查询总rowCount,如下所示:
SELECT
SUM(COUNT)
AS ROWS
FROM
(SELECT COUNT(*) AS COUNT FROM TABLE1
UNION ALL
SELECT COUNT(*) AS COUNT FROM TABLE2
UNION ALL
SELECT COUNT(*) AS COUNT FROM TABLE3
UNION ALL
//ETC...
)
但是它一直在标记语法错误")"。我将如何重组这个?
答案 0 :(得分:1)
DB2(和SQL Server)要求子查询具有别名,因此:
SELECT
SUM(COUNT)
AS ROWS
FROM
(SELECT COUNT(*) AS COUNT FROM TABLE1
UNION ALL
SELECT COUNT(*) AS COUNT FROM TABLE2
UNION ALL
SELECT COUNT(*) AS COUNT FROM TABLE3
UNION ALL
//ETC...
) as subqueryAlias