【发布时间】:2022-01-27 14:37:23
【问题描述】:
在我的 postgres 数据库中,我有一个帖子和 cmets 表:
帖子:
id title
1 post1
2 post2
3 post3
cmets:
id post_id content
1 1 a
2 1 b
3 1 c
4 2 d
5 3 e
6 3 f
7 3 g
如何选择 cmets where post_id in (1,3),但每个 post_id 限制为 2 个,这样我得到:
id post_id content
1 1 a
2 1 b
5 3 e
6 3 f
编辑:对于我的具体情况,我可以通过循环遍历 post_ids 数组以编程方式构造查询。
在想:
(select * from comments where post_id = 1 limit 2)
union all
(select * from comments where post_id = 3 limit 2)
【问题讨论】:
标签: postgresql