给出三个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
@RECORD
是SELECT RECORD FROM MASTER_TABLE WHERE ID=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';