gpt4 book ai didi

sql - 在 SQL 查询中使用表中的值

转载 作者:行者123 更新时间:2023-11-29 13:19:20 25 4
gpt4 key购买 nike

给定三个 SQL 表,
PEOPLE 列 - name (varchar), age (integer)
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

如何实现这一目标?

最佳答案

像这样:

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';

关于sql - 在 SQL 查询中使用表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44515635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com