create table #student(s# int,sname varchar(10),age int)create table #study (s# int, c# char(1),score int)insert into #student
1,'xiaozhu',10 union all select 2,'xiaomao' ,9union all select 3,'xiaozhe' ,7union all select 4,'xiaophai',8 union all select 5,'xiaoduo',9insert #study select 1,'A',99 union all select 1,'B',90 union all select 1,'C',99 union all select 2,'A',99 union all select 2,'b',99 union all select 2,'c',98 union all select 3,'A',99 union all select 3,'b',92 union all select 3,'c',91 union all select 3,'d',90 union all select 4,'A',88 union all select 4,'B',96 insert into #student
1,'xiaozhu',10 union all select 这个union all select
什么意思
然后我用这个sql 执行不了啊 呵呵
1,'xiaozhu',10 union all select 2,'xiaomao' ,9union all select 3,'xiaozhe' ,7union all select 4,'xiaophai',8 union all select 5,'xiaoduo',9insert #study select 1,'A',99 union all select 1,'B',90 union all select 1,'C',99 union all select 2,'A',99 union all select 2,'b',99 union all select 2,'c',98 union all select 3,'A',99 union all select 3,'b',92 union all select 3,'c',91 union all select 3,'d',90 union all select 4,'A',88 union all select 4,'B',96 insert into #student
1,'xiaozhu',10 union all select 这个union all select
什么意思
然后我用这个sql 执行不了啊 呵呵
1,'xiaozhu',10 union all select 2,'xiaomao' ,9union all select 3,'xiaozhe' ,7union all select 4,'xiaophai',8 union all select 5,'xiaoduo',9
要把这些都执行...
1,'xiaozhu',10 union all select 2,'xiaomao' ,9union all select
........
1,'xiaozhu',10 union all select 2,'xiaomao' ,9union all select 3,'xiaozhe' ,7union all select 4,'xiaophai',8 union all select 5,'xiaoduo',9
是这样 加个select就好了但是我的问题是 这个union all select
在这里是什么意思了?
select 1,'xiaozhu',10
union all
select 2,'xiaomao' ,9
你看看这个是什么结果,应该就明白了
1,'xiaozhu',10 union all select 2,'xiaomao' ,9 union all select 3,'xiaozhe' ,7 union all select 4,'xiaophai',8 union all select 5,'xiaoduo',9 是链接了 多个select
的结果形成一个集合
插入到表里 是吗》?
2.union all 一般是将两个select 结果集合并,即使有重复的记录也不会去重
如select 1 union all select 1 union all select 2,结果是3条记录
3.union 与union all 类似,不同的是它会去掉重复的记录,如2中用union 结果就是两条记录示例:
SELECT 1
UNION ALL
SELECT 1
UNION ALL
SELECT 2--result
/*
-----------
1
1
2(所影响的行数为 3 行)*/SELECT 1
UNION
SELECT 1
UNION
SELECT 2--result
/*
-----------
1
2(所影响的行数为 2 行)*/