首页 > 解决方案 > 如何将双引号 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"

标签: sqlpostgresql

解决方案


我还建议将其设为整数(int)。当您在它周围放置替换时它会起作用吗?

replace (trim(data::json ->> 'color_ids','[]'), '"', '')

推荐阅读