我试图写的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