如何在select子句中使用Post in子句(如SQL Server)在Post子句中进行Postg
技术问答
562 人阅读
|
0 人回复
|
2023-09-14
|
我在尝试postgresql以下查询如下:
% G% n& `6 ~& w y- R( L0 I. m5 vselect 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 n1group by name,author_id当然可以Microsoft SQL Server但是在postegresql它根本不可用。我读了它的文档,似乎可以重写为:7 h4 ~% M; J( M$ T. v
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 totalgroup by name,author_id但这在postegresql返回以下错误: FROM中子查询不能引用同级别的其他关系。所以我被卡住了。谁知道我能做到这一点?) u" M% k( X7 d" b2 J2 ?$ A C
谢谢
! W' J, P% m9 L+ y/ U9 T6 R- u: X% c/ E |! p
! z8 c8 l: D1 j 解决方案: 4 z- n! P" C0 G
我不确定我是否完全理解你的意图,但以下内容可能与你想要的非常接近:7 {/ p6 t& D) k( w* ~5 s, G/ u' K% w7 b
select n1.name,n1.author_id,count_1,total_count from (select id,name,author_id,count(1) as count_1 from names group by id,name,author_id) n1inner join (select id,author_id,count(1) as total_count from names group by id,author_id) n2 on (n2.id = n1.id and n2.author_id = n1.author_id)不幸的是,这增加了按压ID以及名称和author_id对第一个子查询进行分组的要求,我认为这不是必需的。不过,我不确定如何解决此问题,因为您需要具有可用的ID加入第二个子查询。也许其他人会提出更好的解决方案。4 M1 |3 c; A; P7 }
分享和享受。 |
|
|
|
|
|