--联合结果集union
select * from Student where Sex='男'
--union
select * from Student where Sex='女'
--联合的前提是:
--1.列的数量需要一致:使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式
--2.列的类型需要可以相互转换
select StudentName,Sex from Student --在字符串排序的时候,空格是最小的,排列在最前面
union
select cast(ClassId as CHAR(3)),classname from grade
--union和union all的区别
--union是去除重复记录的
--union all不去除重复 :效率更高,因为不需要判断记录是否重复,也没有必须在结果庥是执行去除重复记录的操作。但是可以需要消耗更多的内存存储空间
select * from Student where ClassId=2
union all
select * from Student where ClassId=2
--查询office这科目的全体学员的成绩,同时在最后显示它的平均分,最高分,最低分
select ' '+cast(StudentNo as CHAR(3)),cast(SubjectId as CHAR(2)),StudentResult from Result where SubjectId=1
union
select '1','平均分',AVG(StudentResult) from Result where SubjectId=1
union
select '1','最高分',max(StudentResult) from Result where SubjectId=1
union
select '1','最低分',min(StudentResult) from Result where SubjectId=1
--一次性插入多条数据
--1.先将数据复制到另外一个新表中,删除源数据表,再将新表的数据插入到源数据表中
--1.select */字段 into 新表 from 源表
--1.新表是系统自动生成的,不能人为创建,如果新表名称已经存在就报错
--2.新表的表结构与查询语句所获取的列一致,但是列的属性消失,只保留非空和标识列。其它全部消失,如主键,唯一键,关系,约束,默认值
select * into newGrade from grade
truncate table grade
select * from newGrade
--select * into grade from newGrade
--2.insert into 目标表 select 字段列表/* from 数据源表
--1、目标表必须先存在,如果没有就报错
--2.查询的数据必须符合目标表的数据完整性
--3.查询的数据列的数量和类型必须的目标的列的数量和对象完全对应
insert into grade select classname from newGrade
delete from admin
--使用union一次性插入多条记录
--insert into 表(字段列表)
--select 值。。。。 用户自定义数据
--union
--select 值 。。。。
insert into Admin
select 'a','a'
union all
select 'a','a'
union all
select 'a','a'
union all
select 'a',null
12.CASE函数用法
相当于switch case---c#中的switch...case只能做等值判断
这可以对字段值或者表达式进行判断,返回一个用户自定义的值,它会生成一个新列。
2.要求then后面数据的类型一致










