16回答

0收藏

如何在select子句中使用Post in子句(如SQL Server)在Post子句中进行Postgresql子查询?

技术问答 技术问答 1671 人阅读 | 16 人回复 | 2022-03-03

我在尝试postgresql以下查询如下:+ O& v( M9 s* }
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 n1group by name,author_id当然可以Microsoft SQL Server但是在postegresql它根本不可用。我读了它的文档,似乎可以重写为:
  R9 p8 o3 }3 ]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中子查询不能引用同级别的其他关系。所以我被卡住了。谁知道我能做到这一点?
& j; q: E% X% s7 D* E谢谢. ?( ^3 G. @6 O9 X' |! L
                                                               
; r3 z8 D1 J8 e3 l; S& O3 p    解决方案:                                                                2 a6 w& v! v! i% ?' i6 ?5 Q
                                                                我不确定我是否完全理解你的意图,但以下内容可能与你想要的非常接近:5 w2 C* {) p! `( f' R  L- e& f
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加入第二个子查询。也许其他人会提出更好的解决方案。. M# Q: w! o3 h
分享和享受。
分享到:
回复

使用道具 举报

回答|共 16 个

熊宪明

发表于 2022-3-4 00:04:44 | 显示全部楼层

不错 支持下
回复

使用道具 举报

无影灯

发表于 2022-3-5 14:53:28 | 显示全部楼层

回个帖子,下班咯~
回复

使用道具 举报

特别看不上装逼犯

发表于 2022-3-6 15:49:30 | 显示全部楼层

发发呆,回回帖,工作结束~
回复

使用道具 举报

翔源

发表于 2022-3-7 10:02:03 | 显示全部楼层

锄禾日当午,发帖真辛苦。谁知坛中餐,帖帖皆辛苦!
回复

使用道具 举报

时光冉冉

发表于 2022-3-8 05:32:00 | 显示全部楼层

围观 围观 沙发在哪里!!!
回复

使用道具 举报

自由港

发表于 2022-3-8 16:44:18 | 显示全部楼层

找到啦 开心
回复

使用道具 举报

东有羽箭射花雕

发表于 2022-3-8 23:52:44 | 显示全部楼层

我只是路过,不发表意见
回复

使用道具 举报

北柠

发表于 2022-3-9 06:02:48 | 显示全部楼层

我是个凑数的。。。
回复

使用道具 举报

赤脚医生

发表于 2022-3-9 11:16:42 | 显示全部楼层

楼主呀,,,您太有才了。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

29060 积分
9660 主题
热门推荐