- 简单查询
- 条件查询
- where
- between...and...
- in
- like
- 判断为空
- 与,或,非
- 排序查询
- 聚合查询
- 分组查询
- 分页查询
- 多表查询
- 内连接查询
- 隐式内连接
- 显式内连接
- 外连接
- 三表查询
- 自连接
- 子查询
- 常用函数
- MD5加密(扩展)
- 关键字的执行顺序
DQL(Data Query Language) 数据查询语言,就是用来查表中的数据的 简单查询
select * from 表名; //查询指定表中的所有字段的数据 select 字段名1,字段名2 from 表名; //查询指定表中的选定字段的数据 select 字段名 as 别名 from 表名; //显示数据时给字段起别名,其中as可以省略 select distinct 字段名 from 表名; //查询时自动去重 select 字段名+10 from 表名; //显示该字段数据时自动+10,对字符串类型无效,加减乘除均可条件查询 where
select * from 表名 where price = 10; //查询价格price等于10的数据
where后面接条件语句
between…and…select * from 表名 where price between 100 and 200; //查询价格price在100到200之间的数据
100到200为闭区间
inselect * from 表名 where price in(100,200); //查询价格为100或者200的数据like
select * from 表名 where name like '张%'; //查询名字name以张开始的数据 select * from 表名 where name like '_三'; //查询名字name第二个字为三的数据 select * from 表名 where name like '%涛%'; //查询名字name中含有涛字的数据判断为空
is null //为空 is not null //不为空 select * from 表名 where name is not null;
判断为空不能使用=null,在sql里,null不等于null。
与,或,非and //与 or //或 not //非 select * from 表名 where price > 100 and price < 200; select * from 表名 where price < 100 or price > 200; select * from 表名 where not(price < 100 or price > 200);排序查询
select * from order by price asc; //按照价格price升序查询 select * from order by price asc, id desc; //先按照价格price升序查询,再对相同价格数据按照id降序查询
- asc 升序查询,默认为asc
- desc 降序查询
select count(id) from student; //查询学号id不为空的行数 select count(id,name) from student; //查询学号id和姓名name都不为空的行数 select count(*) from student; //查询所有字段都不为空的行数 select sum(id) from student; //查询学号id总和 select max(id) from student; //查询学号id最大的值 select min(id) from student; //查询学号id不为空的行数 select avg(id) from student; //查询学号id的平均值
只能返回单一值,而不是查询字段那种返回多行值
分组查询select max(price) from 表名 group by category_id; //查询相同种类category_id中的最大价格 select max(price) from 表名 group by category_id having max(price) < 100;//查询相同种类category_id中小于100的最大价格 select max(price) from 表名 where id > 10 group by category_id having max(price) < 100;//先对数据进行id大于10的筛选,再查询相同种类category_id中小于100的最大价格 select age,count(age) from 表名 group by id;//查询每个年龄及其对应的人数 select count(*) from 表名 group by id,age;//先按id分组,再对各个分组按age分组,查询id和age都相同的行数
- 分组查询常和聚合查询结合使用
- having是分组后的条件查询
- 分组后相当于获取一张字段为分组查询字段,聚合函数1,聚合函数2.的表,每一行代表每一个分组块的信息
- 由于是分组块,所以个人的数据只有分组查询字段在一个分组块中是一样的,且可以查询
select * from 表名 limit m,n;//从m下标开始查询n条数据
m是下标,并不是第几个
多表查询create table student(
id int,
name varchar(10),
sex varchar(2),
grade_id int
);//学生表
create table grade(
id int,
name varchar(10)
);//年级表
create table score(
student_id int,
student_score int
);//成绩表
内连接查询
隐式内连接
select * from student,grade; //查询student和grade的笛卡尔集,产生一个虚拟表
student有10行数据,grade有5行数据,则虚拟表有50行数据
select * from student,grade where student.grade_id = grade.id;//对虚拟表进行条件筛选,使学生对应正确的年级显式内连接
select * from student inner join grade; //和隐式内连接效果一样,inner可以省略
join常和on搭配使用来进行条件筛选,on的优先级大于where
select * from student join grade on student.grade_id = grade.id;
on后面还可以继续接where
外连接select * from student left outer join grade on student.grade_id = grade.id;//左外连接,outer可以省略 select * from student right join grade on student.grade_id = grade.id;//右外连接
外连接必须和on同时使用
- 内连接结果为符合on条件的所有笛卡尔集
- 左外连接结果为左表全输出,符合on条件的正常输出,不符合on条件的左表正常输出,右表输出为null
- 右外连接结果为右表全输出,符合on条件的正常输出,不符合on条件的右表正常输出,左表输出为null
- 多表查询关键点是找到两个表之间的连接点,也就是相同的字段
select g.name,s.id,score.student_score from student s left join grade g on s.grade_id = g.id left join score on s.id = score.student_id where score.student_score is null;//查询成绩为空的学生的年级和姓名自连接
| sid(子技术id) | fid(父技术id) | name |
|---|---|---|
| 3 | 1 | 软件开发 |
| 5 | 1 | 美术设计 |
| 4 | 3 | 数据库 |
| 8 | 2 | 办公信息 |
| 2 | 1 | 信息技术 |
| 6 | 3 | web开发 |
| 7 | 5 | ps技术 |
现在需要查询有哪几种技术类型和其对应的技术名称
create table tec(
name varchar(20),
sid int,
fid int
);
select a.name '父栏目',b.name '子栏目' from tec a,tec b where a.sid = b.fid;
子查询
select * from student where grade_id = (select id from grade where grade.name = '三班'); select * from (select grade_id,count(*) from student group by grade_id) a;//查询每个班各自有多少人
子查询就是嵌套查询,可以把一个或多个查询结果当作查询对象
常用函数select concat('姓名: ',name) '名字',id from student; //在字段内容输出时,前面加上'姓名: '
其实也不常用,聚合函数还是比较常用的,其他的函数查官网吧
常用函数查询
MD5加密(扩展)create table testmd5(
id int(4),
name varchar(20),
pwd varchar(50),
primary key(id)
);
-- 明文密码
insert into testmd5 values (1,'张三','12345'),
(2,'张二','12345'),
(3,'张六','12345');
-- 加密
update testmd5 set pwd= md5(pwd) where id = 1;
-- 插入的时候加密
insert into testmd5 values (7,'张色弱',md5('12345'));
-- 如何校验:将用户传递进来的密码进行MD5加密,然后和加密后的值对比
select * from testmd5 where name = '张三' and pwd = md5('12345');
关键字的执行顺序
select grade.name,count(student.id) a from student join grade on student.grade_id = grade.id where id >= 2 group by grade_id having a > 1 order by a desc limit 0,5;
from > on > join > where > group by > 聚合函数 > having > select > order by > limit
sql语句中注释为
杠杠空 – 这是注释
或者
斜杠星星斜杠 /这是注释/
本文中因为习惯所以使用//,但这个在sql种是不支持的



