使用子查询将mySQL查询转换为带子查询的DB2 SQL查询

时间:2015-01-29 16:24:37

标签: mysql sql db2

我对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...
)

但是它一直在标记语法错误")"。我将如何重组这个?

1 个答案:

答案 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