当前位置:网站首页>Sql Server之查询总结
Sql Server之查询总结
2022-07-26 10:40:00 【孩纸D】
一:表单查询
1、简单查询列
1)查询所有列
select * from 表名或视图
2)查询指定列
select 列名 [ , . . . ] from 表名 或 视图
3)查询经过计算的值
select 表达式 from 表名 或 视图
例如 select name 2018-age from student; ——> 2018-age 表示当前年份减去年龄,即学生的出生年份
表达式不仅可以是算术表达式,还可以是字符串常量、函数
若是字符串常量则那一列都会表示为指定的字符串
2、查询表中的若干元组
1)取消重复行 ——> distinct
select distinct 列名 from 表名
2)where 条件筛选
select 列名 from 表名 where 条件
① 比较 ——> = > < >= <= != <> !> !<
!= <>这两个都表示不等于; !>不大于; !<不小于
例如:select name , age from student where age < 20
② 确定范围 ——> between . . . and . . . not between . . . and . . .
例如:select name age from student where age between 16 and 20
③ 确定集合 ——> in not int 表示在什么或者不在什么里面
例如:select name , sex from student where sex in ('计算机系' , '数学系' , '英语系')
④ 字符匹配 ——> like
% 可以表示任意长度的(也可以为0)的字符串
_ 表示任意单个字符
\_ 转义字符 表示就是下划线_
例如:select name , no , sex from student where name like '王%'
⑤ 空值的查询 ——> is is not
is 不能用 = 代替的!
例如:select name , no from student where name is not null
⑥ 多重查询 ——> and or
例如:select * from student where name like '_夕%' and age < 20
3、ordey by 字句 ——> 排序的 ASC为升序,desc为降序,默认为升序
例如:select * from student ordey by grade desc , mathG 成绩为降序,若成绩相同则按数学成绩升序
4、集函数
count(*) ——> 统计元组个数
count( [distinct] 列名) ——> 统计一列中值的个数
sum( [distinct] 列名) ——> 计算一列值的总和(此列必须为数值型)
avg( [distinct] 列名) ——> 计算一列值的平均数(此列必须为数值型)
max( [distinct] 列名) ——> 计算一列值中的最大值
min( [distinct] 列名) ——> 计算一列值中的最小值
当集函数遇到空值时,除了count(*),都是跳过空值不对其进行处理,只处理非空值
集函数只能用在 select 字句 和 group by 中的 having 字句中
5、group by 子句
group by 子句是将查询结果按某一列或多列的值分组(一般为一列),值相同的为一组。也就是说是对查询结果进行分组。对分组后的结果进行筛选用 having 。
where 和 having 的作用都是进行筛选的,他们的区别就在与作用对象的不同。where 是作用于基本表或视图的,对元组进行筛选,选出来的是行;having 是作用于组的,筛选出来满足条件的组。
例如:查询学生成绩大于等于90分的学生姓名和平均成绩
select name , avg(grade) from student group by grade having avg(grade)>=90
这个就无法直接利用where条件直接查询。
二:连接查询 ——> 一个查询涉及到两个及以上的表
1、等值与非等值连接查询 ——> 比较运算符
连接运算符为=时为等值连接,其他的为非等值连接
例如:select student . * , sc . * from student , sc where student.no = sc.no ——>将两个表连接起来了,
在等值连接中把目标列中重复属性的列去掉则为自然连接
连接查询会有一个查询先后的关系,通过关系的调整可以对执行过程进行优化。也就是通过对where后面条件的先后关系进行调整,
2.自身连接
一个表与其自己连接,为这个表取两个别名。
例如:查询每一门课的间接先修关系(即先修课的先修课)
con | name | pno(直接先修课) |
1 | 数据库 | 5 |
2 | 数学 | |
3 | 信息系统 | 1 |
4 | 操作系统 | 6 |
5 | 数据结构 | 7 |
6 | 数据处理 | |
7 | Pascal语言 | 6 |
select first.con , second.pno from course first , course second where first.pno = second.con
查询结果为
con | pno |
1 | 7 |
3 | 5 |
5 | 6 |
3、外连接 ——> join | left outer join | right outer join | full join 表名 on 条件
left outer 左外连接,对左边的表不加限制,即会列出左边表的所有元组,同理 right 也是这样的道理。(outer可以去掉)
full join 是全外连接
on后面跟连接的条件,不过一般条件还是跟在where的后面。
4、多表连接
当查询涉及到多个表时,要在where条件中对每个表都进行条件的控制,否则会发生笛卡尔积运算。
5、嵌套查询
一个select - from - where 语句称为一个查询块,将一个查询块嵌套在另一个查询块的where子句或having子句的条件中的查询成为嵌套查询。查询块也可以出现在from短句当中
允许多层嵌套查询。但是,子查询的语句中不能使用order by 子句,order by 子句只能对最终的查询结果查询!然而,若在子查询的select 子句后面添加 top n(n表示一个整数),则可以在子查询中使用order by。
1)不相关子查询
子查询的查询条件不依赖于父查询,执行顺序为先执行内层查询,再执行外层查询。
2)相关子查询
子查询的查询条件用到了父查询,先执行父查询,并且是重复执行的,次数为父查询行数的总行数。
相关子查询可用在select 和 where 子句中,不能用在from子句中。
①、带有 in 谓词的子查询
②、带有比较运算符的子查询
③、带有any/some或all谓词的子查询
语义:>any(大于子查询中的最小值) ; >all(大于子查询中的最大值) ; >=any ; =any(等于子查询中的某个值) ; !=all(不等于子查询中的任何一个值) ; !=any(不等于子查询中的某个值) (其余运算符号类似)
④、带有exists谓词的子查询
带有exists谓词的子查询不返回任何数据,只产生逻辑值 "true" 或 "false"
6、集合查询
并操作 union 交操作 intersect 差操作 except
参加集合操作的个查询结果的列数必须相同,对应项的数据类型也必须相同。
边栏推荐
猜你喜欢
Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2
IAR sprintf 浮点 在UCOS 总格式化成0.0的问题
GIS方法类期刊和论文的综述(Introduction)怎么写?
[leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits
Uniapp uses the simple method signalr (only for web debugging, cannot package apps)
Tradingview tutorial
[leetcode每日一题2021/2/14]765. 情侣牵手
Problems encountered in QRcode QR code (C language)
STM32 Alibaba cloud mqtt esp8266 at command
在神州IV开发板上为STemWin 5.22加入触屏驱动
随机推荐
第6期:大学生应该选择哪种主流编程语言
父类对子类的引用(父类引用指向子类对象)
[leetcode daily question 2021/2/14]765. Lovers hold hands
同步方法中不使用asyncTask<T> 修饰和await获取异步返回值(同步方法中调用异步方法)
Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
[leetcode每日一题2021/8/31]1109. 航班预订统计【中等】差分数组
【dectectron2】跟着官方demo一起做
10 let operator= return a reference to *this
[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array
【机器学习小记】【风格迁移】deeplearning.ai course4 4th week programming(tensorflow2)
[notes on machine learning] [building a cyclic neural network and its application] deeplearning ai course5 1st week programming(keras)
文案秘籍七步曲至----文献团队协作管理
winpcap 抓包函数pcap_loop(),停止问题
json_ object_ put: Assertion `jso->_ ref_ count > 0‘ failed. Aborted (core dumped)
扫雷pro版2021-08-19
A semicolon is missing
一文详解Nodejs中fs文件模块与path路径模块
多目标优化系列1---NSGA2的非支配排序函数的讲解
12 don't forget every component when copying an object
第8期:云原生—— 大学生职场小白该如何学