SQL大量插入基于从另一个表中提取的数据 [英] SQL mass insert based off of data pulled from another table

查看:95
本文介绍了SQL大量插入基于从另一个表中提取的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不知道我的措词是否正确,但我会尽力解释我想要什么.

I don't really know if I worded it correctly, but I'll try to explain what I want.

Table 1 - X
id | name | blah

Table 2 - Y
id | Xid | configKey | ConfigVal

我想做的是,在表2中为表1中的所有内容创建一行,其中表1中的对应id进入表2中的Xid列.我来自Java背景,不太热衷于此sql,所以不太确定如何执行此操作.表1中有很多行,这就是为什么我要编写脚本.

What I want to do is, create a row in table 2 for everything in table 1, where the corresponding id from Table 1 goes into the column Xid in table 2. I'm from a java background and not too keen on sql, so not too sure how to do this. There are a lot of rows in table 1, which is why I would like to script it out.

我非常想做这样的事情:

I pretty much want to do something like this:

Table1 (the object table)
1 test1 a
2 test3 b
3 testn n

运行查询以填充

Table 2 (the config table)
...exisitng rows
59 1 doSomething true
60 2 doSomething true
61 3 doSomething true

因此,我几乎要添加一个配置行(所有相同的值),除了它所对应的ID(表2中的第2列应为表1中的第1列)

So, I pretty much want to add in a config row (all the same values) except for the id it corresponds to (column 2 in table 2 should be col 1 from table 1)

谢谢

推荐答案

要对所有插入的记录使用固定值'doSomething'和'true':

To use fixed values 'doSomething' and 'true' for all inserted records:

insert into table2 (Xid, configKey, ConfigVal)
select id, 'doSomething', 'true'
from table1

这篇关于SQL大量插入基于从另一个表中提取的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆