类型 | 运算符 | 作用 |
---|---|---|
between...and... |
在某一区间(闭区间) | |
in(set) |
在 in 列表中的任一值(非区间);例如:in(100,200) | |
like |
模糊查询,包含 | |
not like |
模糊查询,不包含 | |
is null |
为空 | |
is not null |
不为空 | |
not |
不成立;例如:where not (salary = 5000) | |
比较 | > 、< 、<= 、>= 、= 、!=(<>) |
大于、小于、小于等于、大于等于、等于、不等于 |
逻辑 | and |
多条件同时成立 |
or |
多条件任一成立 |
MyBatis
的 Mapper
文件中 使用运算符会将 <
标记为尖括号
使编译器忽略 []
里内容
$lt;
表示 <
>
表示 >
,在 MyBaits
中 >
可直接使用<=
表示 <=
查询指定字段(column)的数据
,
隔开*
代表查询所有字段
from
:要查询的表where
:筛选字段的条件字段可以参与数学运算
distinct
:去除查询出的完全一样的记录
只能在所有字段最前面
不能用在中间指定某一个字段
as
:指定查询的字段在查询结果中的暂时别名
-- 从 student 表中查询每个学生的总分,显示姓名和总成绩
select [distinct] * /* 或 column1, column2... */ from `table_name` where 限定条件;
-- 语文、数学、英语成绩相加查询显示为一列,取别名 total
select `name`, (Chinese + Math + English) as total from student
like
:模糊查询
%
:表示任意多个字符_
:(下划线)表示任意单个字符not like
like '%A%'; -- 含有A的记录
like '%A'; -- 以 A 结尾
like 'A%'; -- 以 A 开头
like '__A%'; -- 第三个字母为 A,两个下划线
like '%\_%'; -- 转义字符使用,含有 _ 的记录
not like '%A%'; -- 不包含 A 的记录
order by
指定排序的列,排序的的列可以是表中的列名,也可以是指定的别名
asc
升序(默认)desc
降序order by 2
:根据第二个字段排序order by
子句应位于 select
语句的结尾
,
分隔from → where → group by → having → select → order by
select column1 as name from `table_name` order by `column1` /*或 name */ asc /*或 desc*/
-- 查找姓李的数学成绩降序,语文成绩升序排列(显示姓名、数学成绩、语文成绩)
select name,math,chinese from student where name like '李%' order by math desc, chinese asc
null
null
不参与where
语句中
group by
联合使用
group by
在 where
之后执行group by
时整表作为一组执行count(*)
:统计满足条件的总记录条数
count(常量)
效果一样count(字段)
:统计此字段满足条件且不为 null 的记录数select count(*)/* count(列名) */ from `table_name` where 限定条件
-- 统计姓名不为空的数学成绩大于80的总人数
select count(name) from student where math > 80
sum()
返回该列满足 where
条件的行的和
,
不能少select sum(列名), sum(列名) from `table_name` where 限定条件
-- 求出所有人的数学总成绩之和 及 英语和语文总成绩之和
select sum(Math), sum(English + Chinese) from student
select avg(列名), avg(列名) from `table_name` where 限定条件
-- 求一个班数学平均分和总分平均分
select avg(Math),avg(Math + English + Chinese) from student
select max(列名) /* min(列名) */ from `table_name` where 限定条件
-- 求班级最高分和最低分指定为max、min
select max(Math + English + Chinese) as 'max',min(Math + English + Chinese) as 'min' from student
group by
:根据某字段分组
,
分隔
group by
分组时 select
只能查询分组相关字段和 分组函数
Oracle
中语法严格会直接报错MySQL
中语法比较松散可以执行
having
:筛选记录
group by
后面-- group by 分组;分组后使用 having 字句进行过滤
select column1 from `table_name` group by `column` having 限定条件
-- 显示每个部门平均工资大于 2000 的每个岗位的薪资平均值和最低值
-- 先根据部门分组,再根据岗位分组;使用别名筛选,值只计算一次,效率高一些
select avg(salary) as avg_sal, min(salary), deptno, job -- salary 字段未分组,但使用了分组函数
from `table_name`
group by deptno, job
having avg_sal > 2000
-- 该分组查询可以执行,但结果无意义
/*
以 job 分组,但查询了 enamel、sal;这两个字段会随机显示一条记录;结果无实际意义
在 Oracle 更严格的语法规范中无法执行通过
*/
select ename, avg(sal), sal, job
from emp
group by job
多表查询需要使用连接进行查询
SQL 92
,
隔开SQL 99
...join... on...
null
两张表同时查询
处理过滤条件限定查询结果
emp.deptno
emp.deptno = dept.deptno
sal between losal and hisal
-- 语法; inner 可省略
select 字段 from A [inner] join B on 连接条件 where 限定条件
/*
SQL 99 语法,表连接条件 与 where 条件分离
*/
select name, emp.deptno, dname from emp join dept -- 从 emp、dept 两张表中查询显示name、deptno、dname
on emp.deptno = dept.deptno -- 对两张表中相同的字段作为连接条件进行查询
/*
SQL 92 语法,连接条件在 where 中
*/
select name, emp.deptno,dname from emp, dept
where emp.deptno = dept.deptno
-- 语法相同
select ... from A join B on ... where ...
-- 显示员工姓名,工资,工资级别;姓名、工资在表emp,工资级别在表salgrade
select ename, sal, grade from emp join salgrade
on sal between losal and hisal -- 连接条件为最低工资和最高工资之间
将一张表当作两张表使用
-- 同时显示员工名和对应的上级名
-- 将emp表当作两张表分别取别名a、b,同时显示两张表中的ename
select a.ename as '员工', b.ename as '上级' from emp a join emp b
on a.mgr = b.empno -- 连接条件一张表的上级编号等于另一张表的员工编号
即使筛选条件不匹配也全部显示,使用最多
主表满足条件的记录无视连接条件条件全部显示
-- 语法;outer 可省略
左表 left / right [outer] join 右表 on 连接条件
左边表即使连接条件不匹配也全部显示
-- 左外连接,tab1表即使不满足限定条件也显示对应记录
select * from tab1 left join tab2 on 限定条件
右边表即使连接条件不匹配也全部显示
-- 右外连接,tab2表即使不满足限定条件也显示对应记录
select * from tab1 right join tab2 on 限定条件
-- A 先连接 B,连接之后 A 再连接 C
select ...
from A join B
on ...
join C
on ...
join ...
where...
-- 查询每一个员工的部门名称、工资等级、上级领导
select e.ename '员工', d.dname, s.grade, e1.ename '领导'
from emp e join dept d
on e.deptno = d.deptno -- 内连接 dept 表
join salgrade s
on e.sal between losal and hisal -- 内连接 salgrade 表
left join emp e1
on e.mgr = e1.empno; -- 左连接自身
sql
语句中的 select
语句,也叫嵌套子查询where
语句条件select
字句where
子句中where
语句中
in()
操作符:其中一个all()
操作符:满足所有any()
操作符:满足任意一个# 嵌套子查询了:跟 SIMTH 一个部门的员工,不包括SMITH
select ename from emp
where job = (
select job from emp where ename = 'smith'
) and ename != 'smith'
# in 操作符
-- 查询跟10号部门有相同职位的人的姓名、职位、工资、编号,不包括10号部门
select ename, job, sal, deptno from emp
where job in ( -- 在下列的结果之中的其中一个
select distinct job from emp
where deptno = 10 -- 查询在10号部门的职位并去重
) and deptno != 10 -- 不包括 10 号部门
# all 操作符
-- 查询所有工资大于30部门所有人工资的人姓名、工资、部门号
select ename, sal, deptno from emp
where sal > all( -- 大于所有
select sal from emp where deptno = 30 -- 部门编号为 30 的员工的工资
)
-- 或者 where sal > (select max(sal) ...) -- 大于最大值
# any 操作符
-- 查询所有工资大于30部门其中一人工资的人姓名、工资、部门号
select ename, sal, deptno from emp
where sal > any( -- 大于任意一个
select sal from emp where deptno = 30 -- 30 号部门工资
)
-- 或者 where sal > (select min(sal) ...) -- 大于最小值
# 嵌套 select 查询员工名、部门名
select ename, (select dname from dept d
where e.deptno = d.deptno )
from emp e
deptno
, job
) =(select deptno, job from ...
)
deptno = Smith.deptno and job = smith.job
-- 查询所有人中和smith的部门、职位相同的员工
select * from emp
where (deptno, job) = (
select deptno, job from emp
where ename = 'smith' -- 查询simth的职位
) and ename != 'smith'
将子查询的结果当作临时表使用
-- 查询ecs表中各类商品中最高价格的商品名、商品id、价格
select goods_id, ecs.cat_id,goods_name,shop_price
from(
select cat_id, max(shop_price) as max_price -- 先查询ecs表中各类别商品最高价格
from ecs
group by cat_id -- 根据类别分组
) temp join ecs -- 将查询出的结果当临时表来用,取别名 temp;和 ecs 内连接查询
on temp.cat_id = ecs.cat_id and temp.max_pricee = ecs.shop_price -- 连接条件为 id 、价格 相等
合并多个 select 语句的结果
union all
union
可用于毫无相关的表拼接数据
select ename from emp where job = 'MANAGER'
union all
select ename from emp where job = 'SALESMAN';
-- 语法
select ... limit start, rows
-- 从第 start + 1 行开始取,取rows行; start从 0 开始
-- 显示部门id和平均工资
select id, avg(salary) as avg_sal
from employee
where id = 20
group by id -- 根据 id 分组
having avg_sal > 1000 -- 平均工资大大于 1000
order by avg_sal desc -- 各部门内根据平均工资降序
limit 0, 2 -- 显示第一页两行记录;或 limit 2: 默认从 0 开始
执行顺序