最佳答案
我正在试着写一个关于 postgreql 的查询语句:
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 有效,但对 postestresql 则完全没有效果。我读了一下它的文档,似乎可以改写为:
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 中的子查询不能引用相同查询级别的其他关系”。所以我被困住了。有人知道我是怎么做到的吗?
谢谢