我試圖寫的PostgreSQL以下查詢:如何在select子句中使用from SQL語句加入Postgresql子查詢?
select name, author_id, count(1),
(select count(1)
from names as n2
where n2.id = n1.id
and t2.author_id = t1.author_id
)
from names as n1
group by name, author_id
這當然會影響Microsoft SQL Server的工作,但它不是在所有的postegresql。我讀它的文檔了一下,看來我可以把它改寫爲:
select name, author_id, count(1), total
from names as n1, (select count(1) as total
from names as n2
where n2.id = n1.id
and n2.author_id = t1.author_id
) as total
group by name, author_id
但返回上postegresql以下錯誤:「在FROM不能引用的同一查詢級別的其他關係的子查詢」。所以我卡住了。有誰知道我能做到嗎?
感謝
其實好像這應該對Postgres的工作(也許6年以前它沒有:)) – qwertzguy 2016-08-07 23:00:28