简体   繁体   English

JSON 提取多列 PostgreSQL

[英]JSON extract multiple columns PostgreSQL

I had a question earlier: PostgreSQL trim text field with regex (or else) And I got a wonderful answer by a_horse_with_no_name.我之前有一个问题: PostgreSQL 用正则表达式修剪文本字段(或其他)我得到了 a_horse_with_no_name 的精彩回答。 Now I have an additional question regarding this issue.现在我有一个关于这个问题的额外问题。

So here it is this rextester https://rextester.com/SUWG96428 and the goal is to have all the ids in a separate column.所以这里是这个rextester https://rextester.com/SUWG96428 ,目标是将所有ID放在一个单独的列中。 Is it possible at all?有可能吗?

Like this:像这样:

+---+----+-------+-------+
|   | id | ids_1 | ids_2 |
+---+----+-------+-------+
| 1 |  1 |  4202 |  4203 |
| 2 |  2 |  4204 |       |
| 3 |  3 |  4201 |       |
+---+----+-------+-------+

Yep, you can modify your query like:是的,您可以修改您的查询,如:

select 
    t.id, 
    right(((the_column::json->'itemID')->>0)::varchar, 4) as col1,
    right(((the_column::json->'itemID')->>1)::varchar, 4) as col2,
    right(((the_column::json->'itemID')->>2)::varchar, 4) as col3
from the_table t;

DB Fiddle DB小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM