1、三个关联表的查询

use 新建

create table teacher
(
tcode int primary key,
lesson char(10),
age int,
birth datetime
)
go
create table student
(
xcode int primary key,
name varchar(50),
sex char(10),
banji char(10),
yujiao int,
shujiao int,
yingjiao int
)
go
create table score
(
fcode int ,
yufen decimal(18,2),
shufen decimal(18,2),
yingfen decimal(18,2)
)
go
insert into student values(1,'张三','男','一班',1001,1101,1111)
insert into student values(2,'李四','女','一班',1001,1101,1111)
insert into student values(3,'王五','男','一班',1001,1101,1111)
insert into student values(4,'李晨','女','一班',1001,1101,1111)
insert into student values(5,'范爷','男','一班',1001,1101,1111)
insert into student values(6,'邓超','女','二班',1002,1102,1112)
insert into student values(7,'孙俪','男','二班',1002,1102,1112)
insert into student values(8,'李冰冰','女','二班',1002,1102,1112)
insert into student values(9,'nimab','男','二班',1002,1102,1112)
insert into student values(10,'康熙','女','二班',1002,1102,1112)
insert into student values(11,'王凯','男','三班',1003,1103,1113)
insert into student values(12,'姚晨','女','三班',1003,1103,1113)
insert into student values(13,'登s','女','三班',1003,1103,1113)
insert into student values(14,'雍正','男','三班',1003,1103,1113)
insert into student values(15,'baby','女','三班',1003,1103,1113)

insert into teacher values(1001,'一班语文',34,1987-3-23)
insert into teacher values(1002,'二班语文',23,1997-3-23)
insert into teacher values(1003,'三班语文',56,1984-3-23)
insert into teacher values(1101,'一班数学',35,1988-3-23)
insert into teacher values(1102,'二班数学',34,1987-3-23)
insert into teacher values(1103,'三班数学',38,1986-3-23)
insert into teacher values(1111,'一班英语',26,1990-3-23)
insert into teacher values(1112,'二班英语',29,1999-3-23)
insert into teacher values(1113,'三班英语',40,1979-3-23)

insert into score values (1,87,98,59)
insert into score values(2,98.1,67.4,67.9)
insert into score values(3,87.3,67.8,79.9)
insert into score values(4,23,45,78)
insert into score values(5,46,76,57)
insert into score values(6,98,70,60)
insert into score values(7,36,54,45)
insert into score values(8,23,38,82)
insert into score values(9,24,56,70)
insert into score values(10,69,80,78)
insert into score values(11,56,68,92)
insert into score values(12,34,46,68)
insert into score values(13,16,47,83)
insert into score values(14,37,59,71)
insert into score values(15,20,35,74)

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

2、局部变量

--查询数学分数最高的同学的信息
--利用变量查询
select*from student where xcode=(select top 1 fcode from score order by shufen desc)

declare @fcode1 int
select top 1@fcode1=fcode from score order by shufen desc
select*from student where xcode=@fcode1

--查询此次语文成绩最低的学生所任课教师的信息
--变量查询
select*from teacher where tcode=(select distinct yujiao from student where xcode=(select top 1fcode from score order by yufen))

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

declare @banji varchar(50)
select top 1@banji=banji from student join score on student.xcode=score.fcode group by banji order by AVG(yingfen)desc
declare @yingjiao int
select distinct @yingjiao=yingjiao from student where banji=@banji
select*from teacher where tcode=@yingjiao

3、常用全局变量

--常用全局变量 @@****
--@@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转换

4、运算符优先级

--运算符 算数运算符
--+-*/%
--关系运算符
--> < >= <= != !> !<
--逻辑运算符
--and or any between …and exists in like not

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

5、if 语句

--if 表达式
--begin
-- 语句
--end

--else
--begin
--语句
--end

--查看邓凯所教学生有多少及格的
select COUNT(*)from score where fcode in(select xcode from student where yujiao=(select tcode from teacher where name='邓凯')and yufen >60
)

--查看数学分数最高的学生的性别,
--若是男,[这是一个男生]
--否则,[这是一个女生]

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+'正当年'

变量查询,运算符优先级,if语句的更多相关文章

  1. 数据库基础(变量、运算符、if语句、while语句)

    数据库基础(变量.运算符.if语句.while语句)   变量: 定义变量:declare @变量名 数据类型 变量赋值:set @变量名 = 值 输出:print 变量或字符串 SQL语言也跟其他编 ...

  2. PHP语句【变量、运算符表达式、语句】

    一.变量的方法.1.empty可以用empty的方法能够判断变量的值是不是为空.①如果我们看一下某一个变量是不是已经存在过了假如我们输出一下 var_dump (empty($a)); 返回值为tru ...

  3. HTML基础--JS简介、基本语法、类型转换、变量、运算符、分支语句、循环语句、数组、函数、函数调用.avi

    JS简介 1.JavaScript是个什么东西? 它是个脚本语言,需要有宿主文件,它的宿主文件是HTML文件. 2.它与Java什么关系? 没有什么直接的联系,Java是Sun公司(已被Oracle收 ...

  4. js简介、基本语法、类型转换、变量、运算符、分支语句、循环语句、函数、函数调用

    javascript是个脚本语言,需要有宿主文件,他的宿主文件是html文件. 三个常用对话框 alert("")警告对话框,弹出一个警告对话框 <script> al ...

  5. 第二节 Python基础之变量,运算符,if语句,while和for循环语句

    我们在上一节中,我们发现当我们用字符串进行一些功能处理的时候,我们都是把整个字符串写下来的,比如"jasonhy".startwith("j"),如果我们在程序 ...

  6. SQL连接查询、变量、运算符、分支、循环语句

    连接查询:通过连接运算符可以实现多个表查询.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 常用的两个链接运算符: 1.join   on 2.union 在关系数据库 ...

  7. SQL变量、运算符、分支、循环语句

    变量: SQL语言也跟其他编程语言一样,拥有变量.分支.循环等控制语句. 在SQL语言里面把变量分为局部变量和全局变量,全局变量又称系统变量. 局部变量: 使用declare关键字给变量声明,语法非常 ...

  8. java基础基础总结----- 关键字、标识符、注释、常量和变量、运算符、语句、函数、数组(三)

    Java语言基础组成:关键字.标识符.注释.常量和变量.运算符.语句.函数.数组 一.标识符 标识符是在程序中自定义的一些名称,由大小写字母[a-zA-Z],数字[0-9],下划线[ _ ],特殊字符 ...

  9. js变量定义提升、this指针指向、运算符优先级、原型、继承、全局变量污染、对象属性及原型属性优先级

    原文出自:http://www.cnblogs.com/xxcanghai/p/5189353.html作者:小小沧海 题目如下: function Foo() { getName = functio ...

随机推荐

  1. shiro源码篇 - 疑问解答与系列总结,你值得拥有

    前言 开心一刻 小明的朋友骨折了,小明去他家里看他.他老婆很细心的为他换药,敷药,然后出去买菜.小明满脸羡慕地说:你特么真幸福啊,你老婆对你那么好!朋友哭得稀里哗啦的说:兄弟你别说了,我幸福个锤子,就 ...

  2. Mysql加锁过程详解(6)-数据库隔离级别(1)

    Mysql加锁过程详解(1)-基本知识 Mysql加锁过程详解(2)-关于mysql 幻读理解 Mysql加锁过程详解(3)-关于mysql 幻读理解 Mysql加锁过程详解(4)-select fo ...

  3. 再也不用被this苦恼了

    前端编程对于this再熟悉不过了,今日来个老调重弹温故知新,肯定有很多大佬已经完全吃透了this原理,敬请出门左拐.对于理解this似懂非懂的同学可以借鉴一波 1.this描述 this指的是当前执行 ...

  4. Go基础系列:双层channel用法示例

    Go channel系列: channel入门 为select设置超时时间 nil channel用法示例 双层channel用法示例 指定goroutine的执行顺序 双层通道的解释见Go的双层通道 ...

  5. HDUOJ-2089 不要62

    Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来 ...

  6. [转]MySQL修改时区的方法小结

    本文转自:https://www.cnblogs.com/mracale/p/6064447.html 这篇文章主要介绍了MySQL修改时区的方法,总结分析了三种常见的MySQL时区修改技巧,包括命令 ...

  7. [转]RPA流程自动化-Blueprism认证考试介绍

    本文转自:https://www.cnblogs.com/digod/p/9190186.html RPA流程自动化-Blueprism认证考试介绍 接触RPA有一段时间了,几种RPA相关工具也都试用 ...

  8. 使用HttpWebRequest请求API接口以及其他网站资源

    很多时候,我们项目需要其他网站的资源,而这个被请求的网站可能属于你们自己开发管理的网站.也可能是公网上其他网站对外开发的API接口,比如说腾讯的微信公众平台的API接口.各大短信服务商的短信API接口 ...

  9. Java学习笔记之——String和Arrays常用方法

    一.String常用方法 1.subString(int beginIndex,int endIndex) 截取字符串 从beginIndex开始截取,截取endIndex-beginIndex的长度 ...

  10. js 面向对象 ES5 AND ES6

    1. ES5实现 父类: // 职员类 function Employees(id,name,salary) { // 属性 this.id = id; this.name = name; this. ...