sql - 如何将双引号 ID 转换为单引号 ID,以便在 postgres 中可查询?
问题描述
PostgreSQL 版本:10.3
产品示意图
id integer
data text
颜色示意图
id integer
name text
产品的颜色作为 colorids 数组存储在数据中,例如:['1','2']。
我需要一个查询,我可以在其中获取特定产品的颜色 ID 和名称。 尝试了这样的事情并得到了错误:
select id,name from colors where id in (select trim(data::json ->> 'color_ids','[]') from products where id = 1);
错误:运算符不存在:整数 = 文本
我试过这个:
询问:
select data::json ->> 'color_ids' from products where id = 1;
结果:
["2","5","6","14"]
询问:
select trim(data::json ->> 'color_ids','[]') from products where id = 1;
结果:
"2","5","6","14"
解决方案
我还建议将其设为整数(int)。当您在它周围放置替换时它会起作用吗?
replace (trim(data::json ->> 'color_ids','[]'), '"', '')
推荐阅读
- angular - 角元素:包括一次角核心
- wordpress - 为什么会有来自自己IP的XSS攻击?
- php - 更新 Laravel 项目包时出现“找不到类”
- r - numpy“stride”索引的R等价物是什么?
- bash - 在函数之间传递元素。得到阴性结果
- sql-server - 如何使用 parentId 查找 Child 的数量
- node.js - Docker 撰写失败
- python - 第一次迁移到新的 PostgreSQL 数据库。ValueError: int() 以 10 为底的无效文字:''
- asp.net - 如何在asp.net中为sql数据源输入文本'<'和'>'
- java - docker 容器中的 Java 进程在 docker stop 后始终以状态 137 结束