如何在PostgreSQL中构建复杂的查询

时间:2019-05-22 23:12:38

标签: postgresql

我的名为Approvals的表包含以下列: ID,审批者,PendingAtLevel

批准者的样本值看起来像这样[["ash","bash","rash"],["cat","bat","rat"],["boss"]] which is basically a 2d array

Andpuntatatlevel指示我的请求在哪里挂起。如果级别为2的待处理状态,则表示该请求可以由以下"cat","bat" or "rat"中的任何人批准。

以下是我的表格示例数据:

Id  PendingAtLevel  Approvers
1   1   [["ash","bash","rash"],["cat","bat","rat"],["boss"]]
2   2   [["ash","bash","rash"],["cat","bat","rat"],["boss"]]
3   3   [["ash","bash","rash"],["cat","bat","rat"],["boss"]]
4   1   [["ash","bash","rash"],["cat","bat","rat"],["boss"]]
5   1   [["ash","bash","rash"],["cat","bat","rat"],["boss"]]
6   2   [["ash","bash","rash"],["cat","bat","rat"],["boss"]]
7   1   [["ash","bash","rash"],["cat","bat","rat"],["boss"]]
8   2   [["ash","bash","rash"],["cat","bat","rat"],["boss"]]
9   1   [["ash","bash","rash"],["cat","bat","rat"],["boss"]]
10  2   [["ash","bash","rash"],["cat","bat","rat"],["boss"]]
11  3   [["ash","bash","rash"],["cat","bat","rat"],["boss"]]
12  1   [["ash","bash","rash"],["cat","bat","rat"],["boss"]]
13  1   [["ash","bash","rash"],["cat","bat","rat"],["boss"]]
14  2   [["ash","bash","rash"],["cat","bat","rat"],["boss"]]

我正在尝试构建一个api,在其中我可以传递用户名(例如“ boss”)作为输入,它将返回我正在等待“ boss”批准的请求

我想为PostgreSQL查询编写一个sql查询,其中当我提供查询参数boss时,它将返回第3和11条记录。

这是Postgresql9.x.x。我已经尝试了一些基本查询,但无法走得更远。我的专长不是sql。

select Id, PendingAtLevel FROM Approvals WHERE SPLIT_PART(Approvers,'],[',?) LIKE '%boss%' 

我希望看到以下结果:

Id  PendingAtLevel
3       3
11      3

0 个答案:

没有答案