簡體   English   中英

如何將雙引號ID轉換為單引號ID,以便在Postgres中可查詢?

[英]How to convert double quoted ids to single quoted ids so that it is queryable in postgres?

PostgreSQL版本:10.3

產品示意圖

id   integer
data text

顏色圖

id   integer
name text

產品的顏色以色號數組的形式存儲在數據中,例如:['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','[]'), '"', '')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM