postgresql - Slick-pg:如何使用 arrayElementsText 和重叠运算符“?|”?
问题描述
我正在尝试使用slick / slick-pg在scala中编写以下查询,但我对 slick 没有太多经验并且无法弄清楚如何:
SELECT *
FROM attributes a
WHERE a.other_id = 10
and ARRAY(SELECT jsonb_array_elements_text(a.value->'value'))
&& array['1','30','205'];
这是属性表的简化版本,其中value字段是jsonb:
class Attributes(tag: Tag) extends Table[Attribute](tag, "ship_attributes") {
def id = column[Int]("id")
def other_id = column[Int]("other_id")
def value = column[Json]("value")
def * = (id, other_id, value) <> (Attribute.tupled, Attribute.unapply)
}
样本数据:
| id | other_id | value |
|:-----|:-----------|:------------------------------------------|
| 1 | 10 | {"type": "IdList", "value": [1, 21]} |
| 2 | 10 | {"type": "IdList", "value": [5, 30]} |
| 3 | 10 | {"type": "IdList", "value": [7, 36]} |
这是我当前的查询:
attributes
.filter(_.other_id = 10)
.filter { a =>
val innerQuery = attributes.map { _ =>
a.+>"value".arrayElementsText
}.to[List]
innerQuery @& List("1", "30", "205").bind
}
但它抱怨.to[List]
转换。
我试图创建一个SimpleFunction.unary[X, List[String]]("ARRAY")
,但我不知道如何传递innerQuery
给它(innerQuery
is Query[Rep[String], String, Seq]
)。
任何想法都非常感谢。
更新 1
虽然我无法弄清楚这一点,但我更改了应用程序以将 json 字段作为字符串列表而不是整数保存在数据库中,以便能够执行此简单查询:
attributes
.filter(_.other_id = 10)
.filter(_.+>"value" ?| List("1", "30", "205").bind)
| id | other_id | value |
|:-----|:-----------|:------------------------------------------|
| 1 | 10 | {"type": "IdList", "value": ["1", "21"]} |
| 2 | 10 | {"type": "IdList", "value": ["5", "30"]} |
| 3 | 10 | {"type": "IdList", "value": ["7", "36"]} |
解决方案
推荐阅读
- javascript - 使用 replace() 从存储在数组中的每个链接中获取 id?
- oracle - oracle将存储过程的字符串参数转换为数字数组
- mysql - SQL 从另一个表中选择值逗号分隔,其中 id 包含在字符串中
- javascript - 使用 e.stopPropagation() 时复选框更改事件冒泡到父 li 元素
- javascript - 如何删除文本框中的重复值
- ansible - Windows 上的 Ansible:如何更改注册表所有权
- python-3.x - 每晚重采样 DataFrame
- java - 在 Android 中取消缩短 URL
- android - ImageView 中图像背后的黑色背景
- codenameone - 连接请求依赖和顺序