基本语法

select [all|distinct] { | table. | [table.field1 [as alias]]
> 查询表中的所有列
select from students;
> 查询部分列,多列之间使用“,”隔开
select stu_no,stu_name,sex from students;
//可以在列中添加表名:students.stu_no(一般省略)
> 为查询的结果起别名()
select stu.stu_no as 学号,stu.stu_name 姓名 from students as stu;
//关键字:as
//as可以省略
//给表起别名方便区分不同表中的相同列
//给列起别名的目的:
1.中文别名
2.为表达式添加别名,如:
select stu_no+10 ‘new_stu’ from students;
//别名不需要使用引号包裹,遇到保留字符时,正反引号都可以
//任何类表达式都可以取别名(子查询结果也可以)
//对查询结果的列使用别名似乎只有输出的作用
> 去除查询结果中的重复记录
select distinct subj_no from result;
//多余的subj_no数据将会被去除
> 查询条件或者结果带表达式
select 100
100 as resu from dual
select from students where stu_pwd is null
select now(),current_timestamp(),sysdate() from dual
select
from employees where salary>10000/10
//减少在有变量的一边使用表达式,以防止出现不可预期的错误
select md5(‘123’) from dual
> having查询
也是条件查询,和where不同的是,where作为返回前的条件,having相当于对返回结果的过滤

  • having可以结合聚合函数(where是不行的)
    • 查找平均薪资>3000的部门

select department,avg(salary) from salary_info group by department having avg(salary)>3000

模糊查询

//关键字:like
//匹配符:%(代表0-多个字符的占位)
_(代表一个字符的占位)
//不建议使用左模糊(会导致索引失效)
select from subjects where subj_name like ‘高等%’
select
from students where stu_name like ‘李%’
#在几个具体值内(不超过1000个)
//关键字:in
select from subjects where class_hour in (100,111)
#排除查询
select
from students where stu_name not like ‘李%’
select * from subjects where class_hour not in (100,111)
//not in不会排除空值!

查询空值

//关键字:is null或者is not null
//空值不等同于’’或者0!
select * from students where stu_pwd is null
> 使用查询语句作为表达式(子查询)
select stu_no ‘姓“李”同学学号’,stu_result ‘姓“李”同学成绩’ from result
where stu_no in
(select stu_no from students
where stu_name like ‘李%’)
//注:当查询语句作为表达式时必须使用括号将查询语句包裹!
//使用子查询的时候,只能指定一个列

连接查询

//要点:
1.从不同的表中获取多列数据
2.需要的数据在两张表中有连接(两张表中的某个字段相等之类)
//每次数据库查询都会消耗资源,所以需要尽量减少sql的调用次数

内连接

等值连接查询:语法:[inner] join 表 on 连接条件(两张表中相同含义的字段)
//一般多表连接查询需要给表取别名
select s.subj_name,s.grage_id,g.grade_name from subjects s join grade g on s.grade_id=g.grade_id
//建议查询的条件全部加上表别名加以区分(唯一值不必区分,共有值必须区分,否则报错)
#等值查询的等价写法:
select s.subj_name,
s.grade_id,
g.grade_name
from subjects s
, grade g where s.grade_id = g.grade_id
//注:join on并不相当于where的第一个条件,如果需要附加条件,应该先添加where,再使用and,而不是直接在join on 后面添加and
#非等值连接查询(表笛卡尔积,不建议使用,结果为两张表的数据乘积)

外连接

//保证其中一张表的数据是完整的
//外连接实现了一种需求:“想要从多个表中获取数据,确定了某种条件,但是想要其中一张表单的所有数据”

左外连接

//从左表返回所有的记录,即使在右表没有匹配的数据,该数据位使用null代替
//说人话就是在on的查询条件下,再添加所有左表中的其他数据项
select s.subj_name,g.grade_name from subjects s left join grade g on s.grade_id=g.grade_id

右外连接

//从右表返回所有的记录,即使在左表没有匹配的数据

多表连接查询规范:

select * from
表A
left join 表B on 连接条件
left join 表C on 连接条件
//每一次join对应一个条件
where 其他条件

自连接(表自己跟自己关联)

select e2.employee_id,last_name from employee e1 , employee e2 where
e1.manager_id = e2.employee_id
and
e1.last_name=’Chen’
//解读:一个确定姓名的员工的上司的信息

将查询结果排序

//关键字:order by 列 [desc/asc]
//desc:降序 asc:升序(默认)
//写在where后面(如果有where的话)
select * from results order by stu_result desc ,subj_no
//在使用stu_result后降序排序后,使用subj_no升序排序

分页查询

//关键字:limit [起始行(默认为0,第一行)] ,条数
//包含起始行
//在分页查询前必须排序
select * from results order by stu_result limit 1,5;
//从第2条后面查5条
//不需要括号之类
//如果省去第二个参数,那么将返回第一个参数表达数量的结果,比如limit 5,将返回头五个数据

子查询

//在where条件或者from或者select中添加另一次查询(相当于将函数结果返回给条件、范围、需求)
#子查询可能返回一个值,或者多个值
1.[比较运算符>|=|<]子查询,子查询只能返回唯一值
2.
in 子查询,子查询可以返回多个值
//仅仅举个例子,需要列表形式的地方都可以返回多个值
##子查询使用条件(为什么不用连接查询?)
1.查询结果中几个结果出现在几个表中就需要连接几个表
2.缺少的条件,使用子查询替代

  1. select employee_id,
  2. last_name,
  3. salary,
  4. department_id
  5. from employees
  6. where salary > (select salary
  7. from employees
  8. where employee_id =(select manager_id
  9. from employees
  10. where last_name ='Abel'))

//在某些情况下,可以将查询结果组成一张新的表给予from与另一张表连接
//这里容易产生思维定视,select中需要查找的项不一定要from查询的表,只需要符合规则并且满足查询需求即可
//select ,44 from students

select e1.empno,
e1.deptno,
(
select count(1)
from emp
where deptno = (
select deptno
from emp e2
where e2.empno = e1.empno
)
) '部门人数'
from emp e1
where e1.job= 'clerk';


分组查询

//group by 列[,多列](多列的情况下是在当前分组的情况下再分组)
//必须写在where后面
//分组就是根据某些列进行分组(比如序号相同的为一组)
//分组后,每个组只显示一行数据,所以需要对其他列进行处理
//查询结果的列一定是当前分组中所有数据的处理结果

select s.su,s.subj_name,avg(r.stu_result),count(1) from subjects s,result rbj_no
where s.subj_no=r.subj_no
group by s.subj_no
having avg(r.stu_result) >=80

//having用于对分组结果进行过滤,不可以单独使用
//having的效果等同于where,但是因为having在分组之后执行,所以消耗的资源比where少
使用分组查询的情景:
1.需要对结果进行分组
2.结果中出现了多条合并过的数据(比如查询每一个属性的最大值)
//注意这个每一个,单条数据的最大值不需要分组