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. HDU 1006 Tick and Tick 时钟指针问题

    Tick and Tick Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. struts2--使用<s:token></s:token>标签防止重复提交

    取个小例子:这是网页: <body> <s:actionerror/> <s:form action="LoginAction" method=&qu ...

  3. iOS逆向开发(4):注入目标函数 | fishhook | MobileSubstrate | MSHookFunction | iOSOpenDev

    从获得APP的所有类声明,到锁定目标类与函数,现在是时候注入函数了. 所谓"注入函数",小程的意思是让APP执行到小程写的代码中,跟"钩子"的概念一致.小程把个 ...

  4. Yum搭建LNMP环境(动、静、库分离)(week4_day5)--技术流ken

    前言 本篇博客使用yum来搭建lnmp环境,将采用动态,静态以及数据库分开安装的方式即nginx,php,mysql.会被分开安装在不同的服务器之上,搭建出来一套lnmp环境,并部署wordpress ...

  5. SpringCloud学习(一):微服务简介

    一.前情概要 1.单体架构是什么 1).一个归档包包含了应用所有功能的应用程序, 我们通常称之为单体应用. 2).架构单体应用的架构风格, 我们称之为单体架构, 这是一种比较传统的架构风格. 2.单体 ...

  6. zepto的构造器$

    在zepto中,通过$来构造对象 $ = function(selector, context){ return zepto.init(selector, context) } 由该函数,实际上,在调 ...

  7. MEF 基础简介 二

    MEF的导出(Export)和导入(Import) using System; using System.Collections.Generic; using System.Linq; using S ...

  8. c# Cookie,Session,Application,Cache 四种缓存使用情景

    好记性不如烂笔头,记录一下C#缓存使用的情景模式....个人理解,不正之处,欢迎指正 讨论 Cookie,Session,Application,Cache 四种,有的缓存情景对人,有的缓存情景对事儿 ...

  9. [PHP]算法-旋转数组的最小值的PHP实现

    把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组 ...

  10. 解决MyEclipse中install new software问题

    eclipse中点击help可以直接找到install new software选项进行安装插件,但是在Myeclipse中help没有这个选项,如下提供几种解决方法 Windows-preferen ...