请问插入一条记录是这样的嘛:
insert into 表名(字段1,字段2,...) values(值1,值2,...)那下面的是什么意思?怎么也能插入呢。请大侠们帮忙说明下语法,谢谢你们。
insert into t1
select 1,'a' union all
select 1,'a' union all
select 2,'b' union all
select 2,'b' union all
select 2,'b' union all
select 2,'c' union all
select 3,'c' union all
select 3,'c'
go
insert into 表名(字段1,字段2,...) values(值1,值2,...)那下面的是什么意思?怎么也能插入呢。请大侠们帮忙说明下语法,谢谢你们。
insert into t1
select 1,'a' union all
select 1,'a' union all
select 2,'b' union all
select 2,'b' union all
select 2,'b' union all
select 2,'c' union all
select 3,'c' union all
select 3,'c'
go
select 后面的字段个数和类型都要和表t1里面的字段多少和类型一致【自动增量除外】。
select 1,'a' union all
select 1,'a' union all
select 2,'b' union all
select 2,'b' union all
select 2,'b' union all
select 2,'c' union all
select 3,'c' union all
select 3,'c'
结果。
实际上就是相当于
insert into tb
select * from ta
你看一下
select 1,2,3
union all
select 4,5,6
union all
select 1,2,3/*
---------- ----------- -----------
1 2 3
4 5 6
1 2 3
*/
如果不加all
select 1,2,3
union
select 4,5,6
union
select 1,2,3则去除相同的
/*
----------- ----------- -----------
1 2 3
4 5 6
*/
UNION 将数据并在一起
你看一下
select 1,2,3
union all
select 4,5,6
union all
select 1,2,3/*
---------- ----------- -----------
1 2 3
4 5 6
1 2 3
*/
如果不加all
select 1,2,3
union
select 4,5,6
union
select 1,2,3则去除相同的
/*
----------- ----------- -----------
1 2 3
4 5 6
*/