• 变量运算符


    --表一:学生表 student
    --学号:code int (主键)从1开始
    --姓名:name varchar(50)
    --性别:sex char(10)
    --班级:banji char(10)
    --语文教师编号:yujiao int
    --数学教师编号:yujiao int
    --英语教师编号:yujiao int


    --表二:教师表 teacher
    --教师编号:code int (主键) 从1001开始
    --负责课程:lesson char(10)(语文、数学、英语)
    --年龄:age int
    --生日:birth datetime


    --表三:分数表 score
    --学号:code int (学生学号的外键)
    --语文分数:yufen decimal(18,2)
    --数学分数:shufen decimal(18,2)
    --英语分数:yingfen decimal(18,2)


    --学生表数据:插入三个班的学生数据,每个班的相同科目的教师都是相同的
    --至少每个班5个人

    --教师表数据:语文教师,数学教师,英语教师分别三个,每一个对应一个班级

    --分数表数据:学号对应的语文、数学、英语成绩


    create table student
    (
    code int identity(1,1) primary key,
    name varchar(50),
    sex char(10),
    banji char(10),
    yujiao int,
    shujiao int,
    yingjiao int
    )
    go
    create table teacher
    (
    code int identity(1001,1) primary key,
    name varchar(50),
    lesson char(10),
    age int,
    birth datetime
    )
    go
    create table score
    (
    code int,
    yufen decimal(18,2),
    shufen decimal(18,2),
    yingfen decimal(18,2),
    )
    go
    insert into teacher values('王勇','语文',40,'1976-3-15')
    insert into teacher values('石原','数学',42,'1974-3-15')
    insert into teacher values('刘丽萍','英语',30,'1986-3-15')
    insert into teacher values('张晓华','语文',35,'1981-3-15')
    insert into teacher values('李霞','数学',33,'1983-3-15')
    insert into teacher values('李娜','英语',24,'1990-3-15')
    insert into teacher values('陈勇','语文',41,'1975-3-15')
    insert into teacher values('刘邦','数学',37,'1970-3-15')
    insert into teacher values('冯云','英语',29,'1987-3-15')
    go
    insert into student values('张三','男','一班',1001,1002,1003)
    insert into student values('李丽','女','一班',1001,1002,1003)
    insert into student values('陈龙','男','一班',1001,1002,1003)
    insert into student values('邓超','男','一班',1001,1002,1003)
    insert into student values('郑九','男','一班',1001,1002,1003)
    insert into student values('李四','女','二班',1004,1005,1006)
    insert into student values('钱枫','男','二班',1004,1005,1006)
    insert into student values('李娜','女','二班',1004,1005,1006)
    insert into student values('赵康','男','二班',1004,1005,1006)
    insert into student values('王进京','男','二班',1004,1005,1006)
    insert into student values('郭晶晶','女','三班',1007,1008,1009)
    insert into student values('孙茂利','男','三班',1007,1008,1009)
    insert into student values('王龙','男','三班',1007,1008,1009)
    insert into student values('冯巩','男','三班',1007,1008,1009)
    insert into student values('郭丽丽','女','三班',1007,1008,1009)
    go
    insert into score values(1,78,86,70)
    insert into score values(2,83,81,90)
    insert into score values(3,68,76,74)
    insert into score values(4,75,89,80)
    insert into score values(5,89,93,88)
    insert into score values(6,55,66,80)
    insert into score values(7,74,86,60)
    insert into score values(8,90,86,69)
    insert into score values(9,65,58,76)
    insert into score values(10,71,84,75)
    insert into score values(11,68,76,79)
    insert into score values(12,78,67,76)
    insert into score values(13,80,83,77)
    insert into score values(14,78,96,63)
    insert into score values(15,79,84,57)
    go

    --查询此次语文成绩最高的学生的信息
    select * from student where code =(select top 1 code from score order by yufen desc)
    --查询此次数学成绩最高的学生的信息
    select * from student where code =(select top 1 code from score order by shufen desc)
    --查询此次英语成绩最高的学生的信息
    select * from student where code =(select top 1 code from score order by yingfen desc)
    --查询此次语文成绩最低的学生的信息
    select * from student where code =(select top 1 code from score order by yufen )
    --查询此次数学成绩最低的学生的信息
    select * from student where code =(select top 1 code from score order by shufen )
    --查询此次英语成绩最低的学生的信息
    select * from student where code =(select top 1 code from score order by yingfen )
    --查询此次语文成绩最低的学生所任课教师的信息
    select * from teacher where code = (select yujiao from student where code =(select top 1 code from score order by yufen ))
    --查询此次数学成绩最低的学生所任课教师的信息
    select * from teacher where code = (select shujiao from student where code =(select top 1 code from score order by shufen ))
    --查询此次英语成绩最低的学生所任课教师的信息
    select * from teacher where code = (select yingjiao from student where code =(select top 1 code from score order by yingfen ))
    --查询此次语文成绩最高的学生所任课教师的信息
    select * from teacher where code = (select yujiao from student where code =(select top 1 code from score order by yufen desc))
    --查询此次数学成绩最高的学生所任课教师的信息
    select * from teacher where code = (select shujiao from student where code =(select top 1 code from score order by shufen desc ))
    --查询此次英语成绩最高的学生所任课教师的信息
    select * from teacher where code = (select yingjiao from student where code =(select top 1 code from score order by yingfen desc))
    --查询学生信息,将所有语文任课教师编号改为该科目的任课教师名字显示
    select code ,name ,sex,banji,(select name from teacher where code = yujiao) as 语文教师 from student
    --查询学生信息,将所有任课教师编号改为该科目的任课教师名字显示
    select code ,name ,sex,banji,(select name from teacher where code = yujiao) as 语文教师,
    (select name from teacher where code = shujiao) as 数学教师,
    (select name from teacher where code = yingjiao) as 英语教师 from student
    --查询各个学生的学号,姓名,语文分数,数学分数,英语分数,以及三门课里面每一门课的任课教师姓名
    select student.code ,name ,score.yufen,shufen,yingfen ,
    (select name from teacher where code = yujiao) as 语文教师,
    (select name from teacher where code = shujiao) as 数学教师,
    (select name from teacher where code = yingjiao) as 英语教师
    from student join score on student.code =score.code
    --查询每个班级里的语文最高分
    select banji as 班级, MAX(yufen) as 语文最高分
    from student join score on student.code = score.code group by banji
    --查询每个班级里的数学最高分
    select banji as 班级, MAX(shufen) as 语文最高分
    from student join score on student.code = score.code group by banji
    --查询每个班级里的英语最高分
    select banji as 班级, MAX(yingfen) as 语文最高分
    from student join score on student.code = score.code group by banji
    --查看每个班的语文平均分
    select banji,AVG(yufen) from student join score on student.code = score.code group by banji
    --查询语文课程平均分最高的班级的语文教师的信息
    select * from teacher where code =
    (select top 1 yujiao from student where banji =
    (select top 1 banji from student join score
    on student.code = score.code group by banji order by AVG(yufen) desc))
    --查询数学课程平均分最高的班级的数学教师的信息
    select * from teacher where code =
    (select top 1 shujiao from student where banji =
    (select top 1 banji from student join score
    on student.code = score.code group by banji order by AVG(shufen) desc))
    --查询英语课程平均分最高的班级的英语教师的信息
    select * from teacher where code =
    (select top 1 yingjiao from student where banji =
    (select top 1 banji from student join score
    on student.code = score.code group by banji order by AVG(yingfen) desc))

    select * from student
    --变量
    --局部变量
    declare @xingming varchar(50)--声明定义一个varchar类型的叫做@xingming的
    变量
    set @xingming = '赵匡胤'--使用set关键字时一个变量一个变量的赋值
    --声明一个变量和给他设置一个值需要一起执行

    --更改code值为@code的人的名字为@xingming的值
    update student set name=@xingming where code =@code
    use lianxi
    --更改code值为@code值的人的人名为李莲英
    declare @code int
    set @code =17
    update student set name = '李莲英' where code = @code


    --在查询语句中也可以给变量进行赋值
    --取code值为1 的人的姓名
    declare @name varchar(50)
    select @name = name from student where code =1
    select @name

    --在查询语句中也可以给变量进行赋值(不加条件)
    --不加任何条件的情况下执行以下语句,只将最后一行的name复制给@name
    declare @name varchar(50)
    select @name = name from student
    select @name --变量是用来临时存储一个值

    --常用全局变量 @@****
    --@@connections 连接次数
    print @@connections -- 打印出我自从启动sql之后进行了多少次的数据库连

    --@@error 错误
    insert into score values (33,44,55,66)
    print @@error --执行上一个sql语句是有没有错误,没有错误返回0
    --@@language 语言
    print @@language --当前使用的语言
    --@@rowcount row行 count数量
    print @@rowcount --返回上一次执行sql语句
    时在表中影响的行数
    --@@version 版本
    print @@version --所使用的SQL的版本信息


    use Student
    go
    --更改部门职责,查看影响行数
    update bumen set bzhi='负责本公司的产品检验'
    select '总共影响了'+ STR(@@ROWCOUNT) +'行' --STR转换出来有左边的空格
    select '总共影响了'+ CONVERT(varchar(10),@@ROWCOUNT) +'行' --convert转

    select '总共影响了'+ CAST(@@ROWCOUNT as varchar(10)) +'行'
    --cast转换


    use lianxi
    go
    --查询数学分数最高的同学的信息
    --子查询
    select * from Student where code = (select top 1 code from score order
    by shufen desc )
    --利用变量查询
    declare @code int
    select top 1 @code =code from score order by shufen desc
    --声明一个@code变量接收数学分数最高的学生的code
    select * from Student where code = @code
    --拿着刚刚赋值了之后的@code进student表查询

    --查询此次语文成绩最低的学生所任课教师的信息
    select * from teacher where code =
    (select yujiao from student where code =
    (select top 1 code from score order by yufen ))
    --变量查询
    declare @jiaocode int
    select top 1 @jiaocode = code from score order by yufen
    declare @yujiao int
    select @yujiao=yujiao from Student where code = @jiaocode
    select * from teacher where code = @yujiao


    --查询英语课程平均分最高的班级的英语教师的信息
    select * from teacher where code =
    (select top 1 yingjiao from student where banji =
    (select top 1 banji from student join score
    on student.code = score.code group by banji order by AVG(yingfen) desc))
    --变量查询
    declare @banji varchar(50)
    select top 1 @banji= banji from student join score
    on student.code = score.code group by banji order by AVG(yingfen) desc
    declare @yingjiao int
    select top 1@yingjiao= yingjiao from student where banji = @banji
    select * from teacher where code =@yingjiao


    select * from student

    --运算符
    --算数运算符
    -- + - * / %
    --关系运算符
    --> < >= <= != !> !<
    --逻辑运算符
    --and 两个都成立
    --or 两个钟只要一个成立
    --any 任意一个都得成立
    --between..and.. 在...之间
    --exists --存在
    --in 在....里面
    --like 像
    --not 非
    declare @jia int
    set @jia =10%2
    set @jia =4+3
    set @jia = 10-4
    PRINT @jia

    --运算符的优先级
    --第一级:* / %
    --第二级:正好负号(+)(-)
    --第三级:+ -
    --第四级:> < >= <=
    != !> !<
    --第五级:not
    --第六级:and or between and
    --第七级:all any in like
    some exists
    --第八级:=(等号)


    --if 语句,分支语句
    --写法:
    --if 表达式
    --begin
    -- 语句
    --end
    --else
    --begin
    -- 语句
    --end
    --在里面只有一句语句的时候可以省略begin end
    select * from teacher


    --给老师做评教
    --及格人数超过所教人数超过6人就打印优秀,否则,打印教学质量差
    --查看张晓华所教学生有多少及格的(个数)
    declare @sumji int
    select @sumji=COUNT(*) from score where code in
    (select code from Student where yujiao =
    (select top 1 code from teacher where name ='张晓华') and yufen>=60)
    --判断部分
    if @sumji>6
    begin
    print '优秀'
    print '继续保持'
    end
    else
    begin
    print '教学质量差'
    print '好好上课'
    end

    --查看数学分数最高的学生的性别,
    --若是男,[这是一个男生]
    --否则,[这是一个女生]
    declare @sex char(10)
    select @sex= sex from Student where code=(select top 1 code from score
    order by shufen desc)
    if @sex='男'
    print '这是一个男生'
    else
    print'这是一个女生'

    --查出年纪最大的教师的姓名和年龄,如果超过45岁,打印,【***在干几年就退
    休了】
    --如果小于30岁,打印【***你还年轻,继续努力】
    --否则,【***正当年!!】
    declare @tname varchar(50)
    declare @tage int
    select top 1 @tname= name ,@tage= age from teacher order by age desc
    if @tage>45
    print @tname+ '在干几年就退休了'
    else if @tage<30
    print @tname+'你还年轻,继续努力'
    else
    print @tname+'正当年!!'

  • 相关阅读:
    weblogic启动失败java.lang.NullPointerException
    weblogic应用加载不上
    easychm生成帮助文件时出现的目录导航乱码问题
    An error report file with more information is saved as hs_err_pid2756.log
    js parseint
    修改myelipse中部署路径deploy location内容的方法
    FusionCharts制作实时刷新图
    关于Jquery,js脚本加载执行先后顺序的一些事
    jeesite+mysql,数据导入出现问题。
    AMD Radeon HD 7650A显卡问题
  • 原文地址:https://www.cnblogs.com/yuyingming/p/4988881.html
Copyright © 2020-2023  润新知