如何从 return 多语句的查询中将数据插入 table?
How can I insert data into a table from a query which return multiple statement?
INSERT INTO admin(UserID,Username,InitialBid) select ID,username,(select
case
when experience = 'Fundamental' then '1'
when experience = 'Novice' then '2'
when experience = 'Intermediate' then '3'
when experience = 'Advanced' then '4'
when experience = 'Expert' then '5'
end as intbid
from user_details ) from user_details;
在这段代码中,我想将数据从一个 table 添加到另一个 table 以及作为子查询的条件语句列。由于它一次 returns 多行,因此无法执行此查询。
我应该怎么写我们在 InitialBid 列中得到了对应于它的值的值?
我真的怀疑您根本不需要子查询。这不是你想要的吗?
insert into admin (UserID, Username, InitialBid)
select id, username,
case experience
when 'Fundamental' then 1
when 'Novice' then 2
when 'Intermediate' then 3
when 'Advanced' then 4
when 'Expert' then 5
end as intbid
from user_details;
这会为 user_details
中的每一行在目标 table 中插入一行,同时将 experience
转码为文字数字。
备注:
短路case
表达式在这种情况下就足够了(该列只列出一次,在表达式的开头)
initialBid
看起来像一个数字列;如果是这样,请删除 case
表达式生成的文字值周围的单引号,这样数据库就不需要隐式转换它们。
INSERT INTO admin(UserID,Username,InitialBid) select ID,username,(select
case
when experience = 'Fundamental' then '1'
when experience = 'Novice' then '2'
when experience = 'Intermediate' then '3'
when experience = 'Advanced' then '4'
when experience = 'Expert' then '5'
end as intbid
from user_details ) from user_details;
在这段代码中,我想将数据从一个 table 添加到另一个 table 以及作为子查询的条件语句列。由于它一次 returns 多行,因此无法执行此查询。 我应该怎么写我们在 InitialBid 列中得到了对应于它的值的值?
我真的怀疑您根本不需要子查询。这不是你想要的吗?
insert into admin (UserID, Username, InitialBid)
select id, username,
case experience
when 'Fundamental' then 1
when 'Novice' then 2
when 'Intermediate' then 3
when 'Advanced' then 4
when 'Expert' then 5
end as intbid
from user_details;
这会为 user_details
中的每一行在目标 table 中插入一行,同时将 experience
转码为文字数字。
备注:
短路
case
表达式在这种情况下就足够了(该列只列出一次,在表达式的开头)initialBid
看起来像一个数字列;如果是这样,请删除case
表达式生成的文字值周围的单引号,这样数据库就不需要隐式转换它们。