一、查询语句

查询语句的基本语法:

select * from 表名

select 列名,列名... from 表名

注:select * 写法的效率低于 select 列名的写法

select 和 from之间写的内容:

1. 列名加运算符,进行运算。

null值和任何数值做运算的结果都是null

附加:null值和任何字符串连接,结果都是相当于和“”连接

使用 ifnull 函数处理null值的运算 ,Oracle数据库中是nvl函数

-- 查询学生姓名,余额-200的值
select sname,amt-200 from student
-- 如果运算的列含有null值,要处理null值保证运算结果的正确
-- 使用一个mysql的函数进行处理,ifnull函数
-- 函数直接调用,函数一定有结果
-- IFNULL(expr1,expr2) 如果expr1是null那么函数的结果就是expr2 ;否则结果就是expr1
select sname,IFNULL(amt,0)-200 from student

附加函数解释

concat函数:MySQL中是连接字符串的,在Oracle数据库中使用 || 连接字符串

2.为结果集的列定义别名

select sname as n,IFNULL(amt,0)-200 as 余额 from student

注:as可以省略

select sname n,IFNULL(amt,0)-200 余额 from student

3. distinct关键字:去掉重复的行数

select distinct sname,birthday from student
-- 查询出有学生的班级编号
select distinct classid from student

二、条件查询

根据查询条件,查询出部分记录行的数据

语法:

select * from 表名 [where 布尔表达式]

也就是 布尔表达式的结果是true,这行数据才能显示才结果集中

例如:

-- 查询姓名是jack的学生的所有信息
select * from student where sname='jack'

三、查询条件

1.比较运算的条件:: = 、>、< 、= 、!=或<>

-- 查询姓名不是jack的学生的所有信息
select * from student where sname<>'jack'

2.逻辑运算条件:and、or、not

select * from student where not sname='jack'
-- 余额在100~500之间的学生信息
select * from student where amt >= 100 and amt <= 500
-- 余额大于100的 或 身高大于 1.7
select * from student where amt>=100 or height>=1.7
-- and的优先级高于or
select * from student where amt>=100 or height>=1.7 and birthday > '2000-01-01'

and的优先级高于or,也可以使用小括号强制优先级

3. between ... and ... 数值范围条件

-- 余额在100~500之间的学生信息
select * from student where amt between 100 and 500

4.匹配条件/查询条件模糊的

使用like关键字 和 两个符号 % 、_ 组合使用

%和like组合使用代表 0个或更多个

_和like组合使用代表 单个任意字符

例如:

-- 查询名字以rose开头的
select * from student where sname like 'rose%'
-- 查询名字不是rose开头的
select * from student where sname not like 'rose%'
-- 查询名字含有o的学生
select * from student where sname like '%o%'
-- 查询名字第二个字母是o的
select * from student where sname like '_o%'

5.in关键字,集合列表范围查询

-- 查找姓名是jack、rose、tom的学生信息
select * from student where sname = 'jack' or sname = 'rose' or sname = 'tom'
select * from student where sname in ('jack','rose','tom')

6. 空值的判断条件

-- 生日为空的学生信息
select * from student where birthday is null
select * from student where birthday is not null

注:where 语句中不能使用别名

四、排序

排序语句是对查询结果集的排序

使用order by 语句排序

语法:

查询语句的最后 order by 列名或别名,列名或别名

排序的列名后面默认是asc关键字,代表升序desc关键字是降序

-- 查询学生信息,按姓名排序,降序
select * from student order by sname desc
-- 使用别名排序
select sname n from student order by n
-- 组合排序 按身高的升序排序,再按余额的降序排序
select sname,height,amt from student order by height,amt desc

五、limit关键字:限定查询记录

limit语句是在order by 的后面,可以对查询结果条数进行限定

select sname,height,amt from student limit 3
-- 等同于
select sname,height,amt from student limit 0,3
-- 从第1条开始(条数从0开始的)找3条
select sname,height,amt from student limit 1,3

注:分页功能就是用limit实现的

六、分组查询

对查询的数据进行分组,进行一些统计分析的工作。

分组函数/聚合函数

sum()、avg()、max()、min()、count()

select sum(height),avg(height),max(height),min(height),count(height) from student

count(height) 计算身高不为null的记录数,那么要计算学生表中有多少条记录:

select count(0) from student

mysql数据库中分组函数不能嵌套;oracle数据库分组函数最多嵌套两层

查询结果进行分组查询

使用group by  列名,列名....

-- 计算各个班级中的最高身高
select max(height) from student group by classid

下面的SQL语句的数据是不正确的:sname并没有再group by 子句中

-- 计算各个班级中的最高身高
select sname,max(height) from student group by classid

与分组函数一起查询的列,一定要在group by子句中

select classid,max(height) from student group by classid

对两列进行分组查询

-- 计算各个班级中男生和女生的最高身高
select max(height) from student group by classid,sex;
select max(height) from student group by sex,classid

两条语句在结果上是没有区别的

group by中可以使用别名

select classid cid,sex s,max(height) from student group by cid,s;

注:在where语句中不能使用分组函数,可以在having语句中使用分组函数

要做分组函数的条件时,写在having中,不是分组函数的条件写在where中

-- 计算班级平均身高大于1.72的班级
select classid,avg(height) from student
group by classid
having avg(height) > 1.72

七、where和having的区别

where中不能使用分组函数的条件,having中可以使用

where是对每条表记录的条件限定,having是对结果集记录的条件限定。

select classid,avg(height) from student
group by classid
having classid>0select classid,avg(height) from student
where classid>0
group by classid
-- 上面两条语句的区别:
-- 语句1:student表的所有记录按班级分组,计算多个分组的值,对结果集进行having的条件限定获得两条
-- 语句2:student表的班级id大于0的记录按班级分组,计算多个分组的值,获得两条

八、多表连接查询

当查询的列或使用的条件的列,如果在多张表中,可以使用多表连接查询方式。

1. 等值连接

等值连接时,将两张表的所有记录进行组合(笛卡尔积)。组合的数据有一部分是不正确的,要通过查询条件查询出 正确的组合。

-- 查询学生和班级的信息
select * from student,class c
where classid = c.id
-- 查询学生姓名,和班级名称
select sname,cname from student,class c
where classid = c.id
-- 查询学生所有信息,和班级名称
select s.*,cname from student s,class c
where classid = c.id

多张表的多表连接查询

-- 查询员工姓名、部门名称、在部门中的工作年限
select empname,dname,worktime from employee e,department d,work w
where e.empid = w.empid and d.deptno = w.deptno
order by empname
-- 计算员工在公司工作了多少年
select empname,sum(worktime) from employee e,department d,work w
where e.empid = w.empid and d.deptno = w.deptno
group by empname
having sum(worktime)>2
-- 计算部门曾经有多少个员工工作过,显示部门名称和员工数量
select dname,count(0) from work w,department d
where d.deptno = w.deptno
group by w.deptno

2. 不等值连接

-- 查询员工姓名,部门名称 和 年限等级名称
select empname,dname,name from employee e,department d,work w,yearlevel y
where e.empid = w.empid and d.deptno = w.deptno and worktime between low and height

3. 内连接

内连接的结果和等值/不等值连接的结果是相同的。只是写法不同

-- 查询员工姓名、部门名称、在部门中的工作年限
select empname,dname,worktime from work w
inner join employee e on e.empid = w.empid
inner join department d on d.deptno = w.deptno
order by empname
-- 查询年龄大于30岁的员工姓名、部门名称、在部门中的工作年限
select empname,dname,worktime from work w
inner join employee e on e.empid = w.empid and age>30inner join department d on d.deptno = w.deptno
order by empname;select empname,dname,worktime from work w
inner join employee e on e.empid = w.empid
inner join department d on d.deptno = w.deptno
where age>30
order by empname-- 计算员工在公司工作了多少年
select empname,sum(worktime) from work w
inner join employee e on e.empid = w.empid
inner join department d on d.deptno = w.deptno
group by empname
having sum(worktime)>2
-- 计算部门曾经有多少个员工工作过,显示部门名称和员工数量
select dname,count(0) from work w
inner join department d on d.deptno = w.deptno
where d.deptno = w.deptno
group by w.deptno
-- 查询员工姓名,部门名称 和 年限等级名称
select empname,dname,name from work w
inner join employee e on e.empid = w.empid
inner join department d on d.deptno = w.deptno
inner join yearlevel on worktime between low and height

注:内连接/等值/不等值连接问题:只会显示符合连接条件的数据,不会显示所有的数据

4. 外连接

外连接后,可以查询一个表的所有记录。外连接分为左外连接和右外连接,无论是哪种连接,都会显示左表的所有 记录。例如,A表左外连接B表,那么A表就是左表;B表右外连接A表,那么A表就是左表。

左外连接写法:

-- 查询学生和班级的信息 显示所有的学生记录
select * from student s
left outer join class c on s.classid = c.id
-- 查询所有的班级及班级中的学生
select * from class c
left outer join student s on s.classid = c.id
-- 连接多个表时,左外和内联一起写 ,学生表不是左表了
select sname,room from student s
left outer join class c on s.classid = c.id
inner join room r on r.id = c.roomid

5. 自连接

表自己连接自己,就是自连接

-- 查询学生姓名和班长姓名
select * from student s
left outer join student k on s.classking = k.id
-- 查询所有学生的上课地点 和 其班长的上课地点
select s.sname,m.room,k.sname,km.room from student s
left outer join class c on s.classid = c.id
left outer join room m on c.roomid = m.id
left outer join student k on s.classking = k.id
left outer join class kc on k.classid = kc.id
left outer join room km on kc.roomid = km.id

九、子查询

一个查询语句的结果作为另一个查询语句的条件

-- 查询和rose身高相同的学生
-- 外层的是主查询,括号里的是子查询
select sname,height from student
where height = (select height from student where sname = 'rose')
and sname != 'rose'

主查询每进行一行的条件检索,就执行一次子查询语句,如果主查询检索5行,子查询检索4行,一共检索20行 。子查询效率低,但子查询几乎可以解决所有查询需求。

子查询分为单行子查询和多行子查询

子查询的结果只有一行就是单行子查询;反之就是多行子查询

对单行子查询的操作只能是比较操作

对多行子查询的修饰可以是

in:

select sname,height from student
where height in (select height from student where sname = 'rose')

any:

任意一个,只要任意一个符合条件

-- 查询 比2班其中任意一个学生高的学生
select sname,height from student
where height > any(select height from student where classid = 2)
and classid != 2

all:

所有的,要满足子查询所有结果的条件

-- 查询 比2班所有学生高的学生
select sname,height from student
where height > all(select height from student where classid = 2)
and classid != 2

exists:

exists修饰的子查询,如果查询出记录,结果是true,没有查询记录,结果是false

-- 未结课班级的学生
select sname,classid from student
where classid in (select id from class where flag = '0')
-- exists语句替换,效率高于 in语句的子查询
select sname,classid from student s
where exists (select 0 from class c where flag = '0' and s.classid = c.id)

in的子查询 要 替换为exists子查询,效率高

替换的模板写法为

select * from A
where a1 in (select b1 from B)
-- 替换为exists
select * from A a
where exists(select 0 from B b where a.a1 = b.b1)

SQL语句——查询语句相关推荐

  1. mysql五补充部分:SQL逻辑查询语句执行顺序

    mysql五补充部分:SQL逻辑查询语句执行顺序一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SE ...

  2. 如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集

    如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?(2006-12-14 09:25:36) 与这个问题具有相同性质的其他描述还包括: 如 ...

  3. SQL 分页查询语句大全即(查找第N到M条记录的方法)

    SQL 分页查询语句大全即(查找第N到M条记录的方法) 第一种方法,我的原创方法 row=2 表示分页行数 page=1 表示页码 getnum=row*page select * from  (se ...

  4. sqlserver 指定的网络名不再可用_50个比较实用的SQL Server查询语句(1)

    在本文中,我将解释一些通用的SQL查询.我认为每个开发人员都应该了解这些查询.这些查询与SQL的任何特定主题都无关.但是了解这些查询可以解决一些复杂的任务,并且可以在许多场景中使用. 查询1:检索所有 ...

  5. MySQL学习记录 (二) ----- SQL数据查询语句(DQL)

    相关文章: <MySQL学习记录 (一) ----- 有关数据库的基本概念和MySQL常用命令> <MySQL学习记录 (二) ----- SQL数据查询语句(DQL)> &l ...

  6. mysql查询第二个字母为a_MSSQL_关于SQL Server查询语句的使用,一.查询第二个字母是t或者a的 - phpStudy...

    关于SQL Server查询语句的使用 一.查询第二个字母是t或者a的雇员的全部信息 select * from employees where firstname like '_[t,a]%' 注意 ...

  7. mysql基础10(SQL逻辑查询语句执行顺序)

    SQL语句定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOIN <rig ...

  8. SQL 基本查询语句

    这篇博客主要介绍SQl基本查询语句 下面是查询基于的表 Student表 SC表 Course表 下面是具体的代码 1.创建表CREATE TABLE Student( //创建学生表 Sno CHA ...

  9. mysql的查询语句大全_sql语句(sql数据库查询语句大全)

    sql语句 结构化查询语言(StructuredQueryLanguage)缩写为SQL.结构化查询语言是一种数据库查询和编程语言,用于访问数据以及查询,更新和管理关系数据库系统: 程序功能 创建数据 ...

  10. MySQL补充部分-SQL逻辑查询语句执行顺序

    一 SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOI ...

最新文章

  1. 直接广播地址_计算机网络之网桥、冲突域、广播域是什么?
  2. 计算机课做ppt的软件有哪些内容,课程内容计算机软件的基础知识.ppt
  3. Openjudge-NOI题库-和为给定数
  4. mysql数据库2013_MySQL数据库备份(2)2013-6-13
  5. [leetcode]347. Top K Frequent Elements
  6. Python语法糖之:列表解析、集合解析和字典解析
  7. 光纤传感器实验模块_土工离心机试验专用微型光纤孔隙水压传感器
  8. 【Head First Java 读书笔记】(四)对象的行为
  9. 【多目标优化求解】基于matlab蜻蜓算法求解多目标优化问题【含Matlab源码 477期】
  10. C语言读取文件输入输出
  11. 测试方案/测试计划/测试报告
  12. java调用通用对话框_使用通用对话框
  13. 参加百度开放云编程马拉松后一点总结
  14. SP10108 BALLOT - Distributing Ballot Boxes(二分查找)
  15. wps怎么恢复成单页_WPS怎么调回单页显示
  16. 基于Openfire Smack开发即时通讯应用、搭建Openfire服务器(一)
  17. 我们公司使用了 5 年的系统限流方案 ,从实现到部署实战详解,稳的一B
  18. python五大模块_python-5-常用模块
  19. 从小白到鹅厂的通关秘籍(附简历讲解与部分面试题)
  20. 离线强化学习(Offline RL)系列3: (算法篇) IQL(Implicit Q-learning)算法详解与实现

热门文章

  1. android 文件转base64 base64字符串转文件 语音留言及语音播放
  2. Prettier 代码格式化工具使用详解
  3. Hex2Bin使用方法
  4. mPaaS小程序创建
  5. Discrete Cosine Transform Network for Guided Depth Map Super-Resolution
  6. Matlab程序接口应用总结
  7. svn恢复误删文件步骤
  8. 04 Linux软件安装与卸载命令
  9. Ubuntu下搭建UDK2018开发环境
  10. qml textarea出现滚动条