变量查询,运算符优先级,if语句
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语句的更多相关文章
- 数据库基础(变量、运算符、if语句、while语句)
		
数据库基础(变量.运算符.if语句.while语句) 变量: 定义变量:declare @变量名 数据类型 变量赋值:set @变量名 = 值 输出:print 变量或字符串 SQL语言也跟其他编 ...
 - PHP语句【变量、运算符表达式、语句】
		
一.变量的方法.1.empty可以用empty的方法能够判断变量的值是不是为空.①如果我们看一下某一个变量是不是已经存在过了假如我们输出一下 var_dump (empty($a)); 返回值为tru ...
 - HTML基础--JS简介、基本语法、类型转换、变量、运算符、分支语句、循环语句、数组、函数、函数调用.avi
		
JS简介 1.JavaScript是个什么东西? 它是个脚本语言,需要有宿主文件,它的宿主文件是HTML文件. 2.它与Java什么关系? 没有什么直接的联系,Java是Sun公司(已被Oracle收 ...
 - js简介、基本语法、类型转换、变量、运算符、分支语句、循环语句、函数、函数调用
		
javascript是个脚本语言,需要有宿主文件,他的宿主文件是html文件. 三个常用对话框 alert("")警告对话框,弹出一个警告对话框 <script> al ...
 - 第二节 Python基础之变量,运算符,if语句,while和for循环语句
		
我们在上一节中,我们发现当我们用字符串进行一些功能处理的时候,我们都是把整个字符串写下来的,比如"jasonhy".startwith("j"),如果我们在程序 ...
 - SQL连接查询、变量、运算符、分支、循环语句
		
连接查询:通过连接运算符可以实现多个表查询.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 常用的两个链接运算符: 1.join on 2.union 在关系数据库 ...
 - SQL变量、运算符、分支、循环语句
		
变量: SQL语言也跟其他编程语言一样,拥有变量.分支.循环等控制语句. 在SQL语言里面把变量分为局部变量和全局变量,全局变量又称系统变量. 局部变量: 使用declare关键字给变量声明,语法非常 ...
 - java基础基础总结----- 关键字、标识符、注释、常量和变量、运算符、语句、函数、数组(三)
		
Java语言基础组成:关键字.标识符.注释.常量和变量.运算符.语句.函数.数组 一.标识符 标识符是在程序中自定义的一些名称,由大小写字母[a-zA-Z],数字[0-9],下划线[ _ ],特殊字符 ...
 - js变量定义提升、this指针指向、运算符优先级、原型、继承、全局变量污染、对象属性及原型属性优先级
		
原文出自:http://www.cnblogs.com/xxcanghai/p/5189353.html作者:小小沧海 题目如下: function Foo() { getName = functio ...
 
随机推荐
- leetcode — combination-sum
			
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
 - DHCP服务器的搭建
			
dhcp笔记整理:http://services.linuxpanda.tech/DHCP/index.html 1 dhcp简介 DHCP原理 动态主机配置协议(Dynamic Host Confi ...
 - 精读JavaScript模式(八),JS类式继承
			
一.前言 这篇开始主要介绍代码复用模式(原书中的第六章),任何一位有理想的开发者都不愿意将同样的逻辑代码重写多次,复用也是提升自己开发能力中重要的一环,所以本篇也将从“继承”开始,聊聊开发中的各种代码 ...
 - SHELL脚本--简介
			
bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 脚本都以#!/bin/bash开头,“#”称为sharp,“! ...
 - 解读经典《C#高级编程》第七版 Page38-45.核心C#.Chapter2
			
前言 控制流是语言中最基础的部分,我们不谈具体的细节,只讲讲一些关键和有趣的点. 01 流控制 条件语句:if, else if, else if语句的使用非常值得细讲,如何是好的使用习惯.有一点非常 ...
 - Linux常用命令汇总(一)
			
线程操作 1.ps ps -ef 查看当前活动进程 ps -ef | grep [线程关键信息] ps -ef | grep java 查看java相关进程 2.kill killall -9 jav ...
 - sql server查询语句条件判断字段值是否为NULL
			
判断字段是否为null select * from table where c is null select * from table where c is not null 判断字段是否为空 ...
 - C#基础知识总结(五)
			
摘要 其他的数据类型:常量.枚举(enum).结构(struct).数组一.常量 语法:const 类型 变量名 = 变量值 常量称之为值不可变的变量! 在定义的地方赋值,其他的地方不能赋值. 常量变 ...
 - 在SQL中查询某列具有相同值的数据
			
SELECT * FROM dbo.SBD_WAYBILL_GOODS WHERE WG_SW_ID ) ORDER BY WG_ID SELECT * FROM dbo.SBD_WAYBILL WH ...
 - C# winform程序怎么打包成安装项目(VS2010图解)
			
作为研发人员,在本机上开发的winform.wpf或者控制台程序需要发给其他人测试时候,一般需要对其进行打包生成setup安装文件,根据网上查找的资料并结合自己打包成功,记录如下: 注:本程序是一个利 ...