本文共 2418 字,大约阅读时间需要 8 分钟。
select 查询列表
from 表1 别名
【连接类型】join 表2 别名
on 连接条件
【连接类型】join 表3 别名
on 连接条件
...
【where 筛选条件】
【group by 分组】
having 筛选条件
order by 排序列表
连接类型: 1.内连接:inner 2.外连接 用于查询一个表中有的,而另一个表中没有的数据,结果相当于A和B的差集以及A和B的交集。
特点:相当于笛卡尔乘积形式。
出现在其他语句中内部的select语句,称为子查询或内查询。
外部语句可以是insert, update,delete,select等,但是select作为外部语句较多。外面如果为select语句,则此语句称为外查询或主查询特点:
①子查询放在小括号内,括号内不用加分号;
②子查询一般放在条件的右侧; ③标量子查询,一般搭配单行操作符使用,常见的单行操作符有:>, < , >=, <=, <> 列子查询,一般搭配多行操作符使用,如in ,any/some, all ④子查询的执行次序优先于主查询(一) 标量子查询
select *from family.zhutongwhere budget>( #子查询语句 select budget from family.zhutong where name = 'frog' ) and bonus is not null and variety>'a';
(二)列子查询
返回多行,常使用in / not in 操作符,偶尔使用any (some) 和 all。select boys.rank,a.avgfrom boysinner join ( select round(avg(budget),2) as 'avg' from zhutong group by variety) as aon a.avg between boys.low and boys.high;或如下select boys.rank,a.avgfrom ( select round(avg(budget),2) as 'avg' from zhutong group by variety) as a #必须取别名inner join boyson a.avg between boys.low and boys.high;
(三)exists 后面
也称为相关子查询,返回值为bool类型,一般exists 都能够被in代替#existsselect boys.* from boys where not exists( select zhutong.match_id from family.zhutong where boys.id = family.zhutong.match_id);#inselect boys.* from boys where boys.id not in( select family.zhutong.match_id from family.zhutong);
select 查询列表
from 表
【join type表2
on 连接条件
where 筛选条件
group by 分组条件
having 分组后筛选
order by 排序字段 】
limit offset, set
offset 表示要显示条目的起始索引(默认从0开始),size 表示要显示的条目个数
。 特点:要显示的页数 page,每页的条目数size select 查询列表 from 表 limit (page-1)*size,size
SELECT emp.*FROM empORDER BY EMPNO DESCLIMIT 5; #倒数5项
union all
select * from empwhere year(emp.HIREDATE) > 1980or emp.COMM is not null;#等价于select * from empwhere year(emp.HIREDATE) > 1980union select * from emp where emp.COMM is not null;
执行顺序
感谢诸君观看,如果感觉有用的话,点个赞吧!🎉
转载地址:http://ibcvz.baihongyu.com/