SqlServer 学习笔记

随机函数

  select rand()

    declare @age int

    set @age = rand()*100

    select @age

数据类型转换

  declare @birthday datatime

  set @birthday = getdate()

  select convert (varchar,@birthday)

时间函数

  select getdate()

  select convert(varchar(4),Year(getdate()))

  select dateadd(dd,2(getdate())) 日

  select dateadd(mm,2(getdate())) 月

       select dateadd(yy,2(getdate()))   年

  select datediff(day,'2005-3-4',(getdate()))  计算给定日期和现在日期相差天数

流程控制语句

  判断语句

  if 条件…… else……

  case  条件  when  5  then ……

       when 6 then……

          else …… end

  case when @scroe<60 then ....

 

  循环 while (条件)

    begin

    ……

    end

实例:

declare @time int, @a varchar(100) set @time=0 set @a='*' while (@time<20) begin print @a set @a+='*' set @time+=1 end

动态构造SQL语句

  declare @tableName nvarchar(20)

  set @tableName='T'+convert(varchar(4),Year(getdate()))+'年'+convert(varchar(2),Month(getdate()))+'月'+convert(varchar(2),day(getdate()))+'日'

  execute (' create table ' +@tablename+' (studentid int, studentName nvarchar(10)) ')

过滤数据

  使用比较操作符 = > < >= <= <>

  使用字符比较符 like

  % 0个或多个字符串

  —任何单个的字符

  【】在指定区域或集合内的任何单个字符

  【^】不在指定区域或集合内的任何单个字符

  select * from tsudent where sname like '高%'

  select * from tstudent where sname like '_[明,宇]_'

查询未知的值

  select * from tstudent where class is null     --查找班级为空的学生

  select * from tstudent where class is not null   --查找班级不为空的学生

格式化结果集

  排序

  asc  升序   desc降序

       实例: select * from tscore order by mark desc

       去重  distinct

   select distinct class from tstudent

 多表查询

  内连接  select a.*,b.* from student a join score b on a.studentid=b.studentid

       左右连接   select a.*,b.* from student a left (right)join score b on a.studentid=b.studentid

数据分组和汇总

TOP n 列出前n行记录

   select top 5 * from tstudent order by Birthday

 

使用聚集函数

count(*) count(列) sum min max avg

  select count(*) from tstudent

  统计表中class列不为空的记录数量   select count(class) from tstudent

  select avg(mark) from tscore where subjectid=1

group by 使用例子

  select Class,count(*) from tstudent group by Class

        select sname,avg(mark) from tscore a join tstudent b on a.studentid=b.studentid group by sname having avg(mark)>77    --having 相当于条件

  多列汇总

  select subjectname,class,sum(mark) from tstudent a join tscore b on a.studentid=b.studentid join tsubject c on b.subjectid=c.subjectid group by subjectname,class with rollup  -- 联合使用group by 子句和操作符rollup,将两列的详细信息和分组汇总

      联合使用cube,将对列出subject那么,class两列进行汇总

  select subjectname,class,sum(mark) from tstudent a join tscore b on a.studentid=b.studentid join tsubject c on b.subjectid=c.subjectid group by     subjectname,class with cube

查找网络管理课程的学生成绩

   select studentid,subjectname, mark from tscore a join tsubject b on a.subjectid=b.subjectid where subjectname='网络管理'

         另一种写法
   
select studentid, sname from tstudent where studentid in (select studentid from
   (select studentid,avg(mark) trt  from tscore group by studentid having avg(mark)<60) as t1)

 数据增删改

插入

  insert tstudent values('0000001902','hanligang','男','1414121548554236565','1984-5-12','3234@qq.com','网络班',getdate())

  insert tstudent (studentid,sex,name) values ('454514545','man','jkjdkf')   -- 插入部分数据

  insert tnetwork select * from tstudent where class='网络班'   --将查询的数据插入现有表中

   select studentid,sname,sex,email into TDev from tstudent where calss='网络班'    --将查询的记录创建新的表

删除

  delete tstudent where studentid='24234234'

  delete tstudent where birthday <'1982-1-1'

  delete tstudent where studentid in (select distinct studentid from tscore where mark<60)

  delete tstudent where studentid in (select distinct a.tstudentid  from tstudent a join tscore  b on a.studentid=b.studentid where mark<60)

       等价于    delete tstudent from tstudent a join tscore b on a.studentid=b.studentid where mark<60

更新

  update tstudent set sname='hanlignag' where studentid='000000569'

  update tscore set mark=mark+5 from tscore a join tsubject b on a.subjectid=b.subjectid where b.subjectname='软件测试‘ and mark<80

 

 

  

 

posted on 2019-06-25 13:33  discover_dev  阅读(180)  评论(0编辑  收藏  举报

导航