json - 如何从具有 json 列条件的表中进行选择?
问题描述
我contact_information
在 PostreSQL 9.6 有一张桌子。
CREATE TABLE contact_information
(
employee_id UUID NOT NULL PRIMARY KEY,
messengers JSON NOT NULL
);
INSERT INTO contact_information
(employee_id, messengers)
VALUES
('8b5fbfec-d213-426c-86fa-4fda5f586319', '[{"Type":"skype","IDs":["some skype id","another skype id"]}]');
我需要选择 ID 包含子字符串的所有employee_id,例如“id ILIKE '%other%'”。
我尝试了什么:
SELECT
ci.employee_id,
json_array_elements(json_array_elements(ci.messangers)->'IDs'::text)::text AS ids
FROM contact_information AS ci
并得到:
3ba6feba-ff81-11e4-9408-984be16afda1 "some skype id"
3ba6feba-ff81-11e4-9408-984be16afda1 "another skype id"
但我不能添加到语句 WHERE ids ILIKE '%other%' - 列 ID 不存在。
如果我正在尝试
SELECT
ci.employee_id
FROM contact_information AS ci
WHERE json_array_elements(json_array_elements(ci.messangers)->'IDs'::text)::text ILIKE '%other%'
得到:错误:在 WHERE 中不允许设置返回函数。
有什么想法可以解决这个问题吗?
解决方案
解决了:
SELECT DISTINCT t.employee_id
FROM (
SELECT
ci.employee_id,
json_array_elements(json_array_elements(ci.messangers)->'IDs'::text)::text AS ids
FROM contact_information AS ci
) as t
WHERE t.ids ILIKE '%other%'
对不起一个愚蠢的问题=(
推荐阅读
- r - 错误:“as.Date(20110505, %Y%”中出现意外 SPECIAL
- angularjs - 带有函数作为文件名的 img src
- powershell - 带有 CosmosDB 的 Powershell Azure 函数
- beautifulsoup - 虚拟环境中没有名为“bs4”的模块
- r - 如何对齐交叉栏中的标签
- java - 如何在 2 个类中使用枚举并将 Java 特定类型存储在属性文件中
- css - 在所有现代浏览器中工作相同的背景图像之间的转换?
- python - CartoPy 中绘制轮廓的插值方法
- reactjs - 如果输入已提交,则重定向到不同的路由 - React Router
- node.js - 在 ReactJS 中登录外部网站后如何捕获 URL?