SQL Server之 (三) 分组 类型转换函数 插入多条数据 字符串函数

SQL Server之 (三)  分组 类型转换函数  插入多条数据  字符串函数

自己学习笔记,转载请注明出处,谢谢!---酸菜

1.SQL 数据分组----Group by /Having 

    ①有一学生信息表:StuID/StuName/StuAge/StuGender/StuClassID;

       求每个班级的ID和对应人数:select StuClassID 班级ID,count(*) 班级人数  from StuInfo group by StuClassID ;

       求所有同学中男女同学对应人数:select StuGender 性别, count(*) 人数 from StuInfo group by StuGender ;

       求每个班级ID对应每个班级的男同学人数: select StuClass 班级, count(*) 男同学人数 from StuInfo where StuGender='男' group by StuclassID ;

       执行顺序是:

                     select StuClass 班级, count(*) 男同学人数-------------------4

                                                           from StuInfo--------------------1

                                               where StuGender='男'-------------------2

                                                  group by StuclassID ------------------3

    ②当使用了分组语句(group by )/聚合函数时,在select 查询中不能再包含其他列名,只能放到聚合函数里才能出现;

    ③where 和 having 的区别:

       where:对没有分组前的每列数据筛选,后可跟任何列且不可跟聚合函数;

       having:对分组后的每一组数据进行筛选,后只可跟分组列/聚合函数; 

       执行顺序:

                      select StuClass 班级, count(*) 男同学人数------------------5

                                                           from StuInfo--------------------1

                                               where StuGender='男'-------------------2

                                                  group by StuclassID ------------------3

                                                   having 男同学人数>2 -----------------错误写法,因为还未重命名;

                                                     having count(*)>2 ------------------4

2.SQL 处理顺序分析

    ①from

    ②on

    ③join

    ④where

    ⑤group by

    ⑥with cube/with rollup

    ⑦having

    ⑧select

    ⑨distinct

    ⑩order by 

    ⑪top 

3.类型转换函数

    Cast(表达式 as 数据类型 )

    convert(数据类型,表达式 )

    select '您班级编号是:' + convert(char(1),1)

    select 100.0 + cast(int,'100')

4.联合结果集union (集合运算符)

    union和union all 都能进行联合,区别:union 可以联合去除重复且重新排序; union all 不会去除重复且不会排序; 

    大多数情况下,联合时候不需要去除重复,同时要保持原有数据的顺序,一般建议用union  all 

    union因为要重复扫描,所以效率会很低;

    select  stuName,stuAge,stuID from studentInfo 

    union (all) 

    select techName,techAge ,techID from teacherInfo

    可使用union向表中插入多条数据,且union也会排重

5.备份数据

    select * from StudentInfo 

    select * into StudentInfo20170404BackUp from StudentInfo 

    将表StudentInfo表的结构和表中数据备份到 StudentInfo20170404BackUp中,且表StudentInfo20170404BackUp是在执行select  into 语句时候创建的,但原表的约束不会被备份过去;

    所以select into 语句不能重复执行,因为每次执行都会创建表

    原表的结构包括自增列都会在备份表创建,但是原表的约束不会出现在备份表中;

    只取表结构不取值: select top 0 * into backupTable from table    

6.字符串函数

    ①len() 计算字符个数,不分中英文,只数字符个数;

      print  len('Hi~最近好么?')   8

    ②datalength()  返回所占用字节的个数,这个不是字符串函数;

      print  datalength('Hi~最近好么?')   12个,中文两个字节,英文一个自己

      print  datalength(N'Hi~最近好么?')  所有都是取Unicode方式,都按2个字节算

    ③upper 转大写/lower 转小写

      print upper('Hello,How are you?')

      print lower('Hello,How are you?')

    ④ltrim 去左端空格/rtrim 去掉右端空格 /ltrim(rtirm()) 去掉两端空格
    ⑤字符串截取函数

      <1> left() 从左数开始截取:   print left('中华人民共和国',2)   中华

      <2> right()  从右数开始截取: print right('中华人民共和国',2)   和国

      <3> substring()    print substring('中华人民共和国',1,3)   从第一个位置开始,截取3个:中华人

                                 print substring('中华人民共和国',-2,3)  空

7.日期和时间函数

    ①获取当前日期和时间:  getdate()/sysdatetime()(精度相对更高)

    ②在某一时间再加:dateadd(datepart,num,date)  dateadd(day/month/year/minute/second/hour,200,getdate())              

    ③两个日期差:datediff(datepart,date1,date2)  date1<date2=正数,date1>date2=负数

    ④获取日期的某部分的值:datepart(year/month/day/hour/minute/second,getdate())  返回int 类型;

       

 

posted @ 2017-04-04 18:46  酸菜sauerkraut  阅读(493)  评论(0编辑  收藏  举报