使用SQL查询中的表中的值

时间:2017-06-13 08:03:43

标签: sql json postgresql

给出三个SQL表,
PEOPLE列,列为name(varchar),age(整数)
INFO包含列 - email(varchar),phone(varchar)
master_table为 -

ID  |  TABLENAME     | RECORD  
--- | -------------- | ------------------------------------------------------------------  
1   |      PEOPLE    | "{"name": "Mary", "age": 12}"  
2   |      INFO      | "{"email": "xyz@abc.com", "phone": "321-456"}"  

现在,我希望编写一个查询,将master_table中的数据加载到相应的表中。有什么东西 -

INSERT INTO @TABLENAME VALUES @RECORD AS JSON 'AUTO'  

@TABLENAME在哪里说SELECT TABLENAME FROM MASTER_TABLE WHERE ID=1 @RECORDSELECT RECORD FROM MASTER_TABLE WHERE ID=1

如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

这样的事情:

insert into people (name, age)
select record::json ->> 'name', (record::json ->> 'age')::int
from master_table
where tablename = 'PEOPLE';

insert into info (email, phone)
select record::json ->> 'email', record::json ->> 'phone'
from master_table
where tablename = 'INFO';