MySQL查询:
SELECT *
FROM
(
(SELECT count(*) AS TC FROM tc where pID='22') AS tCount,
(SELECT count(*) AS SC FROM sc where pID='22') AS sCount,
(SELECT count(*) AS RS FROM results where pID='22'and isDeleted=false and status<>'Running')as rCount
)
但是这对PostgreSQL不起作用。我收到这个错误:
在tCount附近的位置92处的语法错误
即。错误发生在','。
我们如何将此查询转换为PostgreSQL?我尝试在PostgreSQL中使用WITH
查询,但我没有得到满足语句要求所需的脚本。
我试过这个PostgreSQL翻译的查询:
WITH tCount AS
(
SELECT count(*) AS TC FROM tc where pID='22'
), sCount AS
(
SELECT count(*) AS SC FROM sc where pID='22'
), rCount AS
(
SELECT count(*) AS RS FROM results where pID='22'and isDeleted=false and status<>'Running'
)
我的方法是否正确?如果是,那么我该如何完成此脚本。如果没有,那么上面的MySQL查询到PostgreSQL查询的翻译是什么?
答案 0 :(得分:1)
试试这个(未经测试):
SELECT
(SELECT COUNT(*) FROM tc WHERE pID='22') AS tCount,
(SELECT COUNT(*) FROM sc WHERE pID='22') AS sCount,
(SELECT COUNT(*) FROM results
WHERE pID='22' AND isDeleted=false
AND status<>'Running') AS rCount
我刚刚将子查询虚拟表更改为子查询虚拟列,修复了一些语句的整理情况,并删除了一些不需要的列别名。
编辑:我的原始答案以DUAL
表为特色,这是一些数据库系统上的虚拟表,用于完成不直接从真实表中选择的查询。由于它在您的情况下不起作用,我已将其删除 - 并且根据维基百科的文章,无论如何在PostgreSQL中都不需要FROM
子句。
答案 1 :(得分:1)
你不需要外支架。这有效:
SELECT *
FROM
(SELECT count(*) AS TC FROM tc where pID='22') AS tCount,
(SELECT count(*) AS SC FROM sc where pID='22') AS sCount,
(SELECT count(*) AS RS FROM results where pID='22'
and isDeleted=false
and status<>'Running')as rCount
请注意,列名是&#34; tc&#34;,&#34; sc&#34;和&#34; rs&#34;。如果你想要&#34; tCount&#34;等你必须在count(*)
语句之后更改别名。