sql 插入另一表的数据
来源:百度知道 编辑:UC知道 时间:2024/05/15 10:11:36
insert into 表1 (列1) values (select 列 from 表2 group by 列)
连续插入数据
要是插入多个列呢
insert into 表1 (列1,列2,列3) values (select 列 from 表2 group by 列)对应列2 对应列3
__________________
列2插入0
列3插入1
__________
2楼 列1是通过查询语句得到,列2,列3是定值,可以么
连续插入数据
要是插入多个列呢
insert into 表1 (列1,列2,列3) values (select 列 from 表2 group by 列)对应列2 对应列3
__________________
列2插入0
列3插入1
__________
2楼 列1是通过查询语句得到,列2,列3是定值,可以么
insert into 表1 (列1,列2,列3) values (select 列1,列2,列3 from 表2 group by (列1,列2,列3))
这样写就好了:
insert into 表1 (列1,列2,列3) select 列3, 0, 1 from 表2 group by 列3
关键点:后面Select 子句中的字段列表与前面 Into 子句中的字段类型一一对应。
如果是要插入的数据某字段为定值,只需要在Select子句字段列表相应位置直接写上值。注意类型,字符串的话要用引号。
insert into 表1 (列1,列2,列3) select 列,0,1 from 表2 group by 列