DQL(数据查询语言)总结
一. 创建stu表
CREATE TABLE stu (
id int, -- 编号
name varchar(20), -- 姓名
age int, -- 年龄
sex varchar(5), -- 性别
address varchar(100), -- 地址
math double(5,2), -- 数学成绩
english double(5,2), -- 英语成绩
hire_date date -- 入学时间
);
二. 添加数据
INSERT INTO stu(id,NAME,age,sex,address,math,english,hire_date)
VALUES
(1,‘马运’,55,‘男’,‘杭州’,66,78,‘1995-09-01’),
(2,‘马花疼’,45,‘女’,‘深圳’,98,87,‘1998-09-01’),
(3,‘马斯克’,55,‘男’,‘香港’,56,77,‘1999-09-02’),
(4,‘柳白’,20,‘女’,‘湖南’,76,65,‘1997-09-05’),
(5,‘柳青’,20,‘男’,‘湖南’,86,NULL,‘1998-09-01’),
(6,‘刘德花’,57,‘男’,‘香港’,99,99,‘1998-09-01’),
(7,‘张学右’,22,‘女’,‘香港’,99,99,‘1998-09-01’),
(8,‘德玛西亚’,18,‘男’,‘南京’,56,65,‘1994-09-02’);
三、基础查询
- 语法
SELECT
字段列表
FROM
表名字段
WHERE
条件列表
GROUP BY
分组字段列表
HAVING
分组后的条件列表
ORDER BY
排序字段列表
LIMIT
分页参数
-
查询name、age 两列
SELECT name,age FROM stu;`![查询name、age 两列](https://img-blog.csdnimg.cn/9a062381a5d34688b488be1ea7d6b3a1.png)
-
查询所有列的数据,列名的列表可以使用*替代,(建议不要使用)
SELECT * FROM stu;
- 查询地址信息
select address from stu;
去除重复记录(distinct)
select DISTINCT address from stu;
5. 查询姓名、数学成绩、英语成绩
SELECT name,math,english from stu;
使用 as 来起别名
SELECT name,math as 数学成绩,english as 英语成绩 from stu;
四、条件查询
- 查询年龄大于20岁的学员信息
select * from stu where age > 20;
2.查询年龄大于等于20岁的学员信息
select * from stu where age >= 20;
3.查询年龄大于 20 并且 小于等于 30 岁学员的学员信息(sql语句中 and 和&& 含义相同)
select * from stu where age >=20 && age <=30;
select * from stu where age >=20 and age <=30;
select * from stu where age between 20 and 30;
4.查询入学日期在’1998-09-01’到’1999-09-01’ 之间的学员信息(sql中日期也是可以用大小进行比较的)
select * from stu where hire_date BETWEEN '1998-09-01' and '1999-09-01';
5.查询年龄等于18岁的学员信息(一个 = 就可以了,和java不一样)
select * from stu where age = 18;
6.查询年龄不等于18岁的学员信息( <> 也代表不等号)
select * from stu where age != 18;
select * from stu where age <> 18;
7.查询年龄等于18岁或者 年龄等于20岁 或者 年龄等于22岁 的学员信息
select * from stu where age = 18 or age = 20 or age = 22;
select * from stu where age in(18,20,22);
8.查询英语成绩为null 的学员信息
(注意:null值的比较不能使用 = != 。需要使用 is is not)
select * from stu where english is null;
select * from stu where english is not null;
五、模糊查询 like
1.通配符:
_:代表单个任意字符
%:代表任意个数字字符
2.查询姓’马’的学员信息
select * from stu where name like '马%';
3.查询第二个字是’花’的学员信息
select * from stu where name like '_花%';
4.查询名字中包含’德’的学员信息
select * from stu where name like '%德%';
六、排序查询
1.语法:
SELECT 字段列表 FROM 表名 ORDER BY 字段1 排序方式1, 字段2 排序方式2;
2.排序方式:
ASC:升序排列(默认值)
DESC:降序排列
3.查询学生信息,按照年龄升序排列
select * from stu order by age ASC;
4.查询学生信息,按照数学成绩降序排列
select * from stu order by math DESC;
5.查询学生信息,按照数学成绩降序排列,如果数学成绩一样,再按照英语成绩升序排列(多字段排序)
select * from stu order by math desc,english ASC;
七、分组查询
1)聚合函数:
* count:统计数量
* 取值:
1.主键
2. *
* max:求最大值
* min:求最小值
* sum:和
* avg:求平均值
2)求值
–1.统计班级一共有多少个学生(count 统计的列名不能为null)
select count(id) from stu;
–2.查询数学成绩的最高分
select max(math) from stu;
--3.查询数学成绩的最低分
select min(math) from stu;
–4.查询数学成绩的总分
select sum(math) from stu;
–5.查询数学成绩的平均分
select avg(math) from stu;
–6.查询英语成绩的最低分
(因为英语成绩中有一个为null,null值不参与所有聚合函数的运算)
select min(english) from stu;
八、分组查询
1.语法:语法:SELECT 字段列表 FROM 表名 [ WHERE 分组前条件限定 ] GROUP BY 分组字段名 [ HAVING 分组后的过滤条件 ];
2.查询男同学,女同学各自的数学平均分
select avg(math) from stu group by sex ;
select sex,avg(math) from stu group by sex ;
3.查询男同学,女同学各自的数学平均分,以及各自人数
select sex,avg(math),count(*) from stu group by sex ;
4.查询男同学,女同学各自的数学平均分,以及各自人数,要求:分数低于70分的不参与分组(分组之前进行分数的判断)
select sex,avg(math) from stu where math >70 group by sex ;
5.查询男同学,女同学各自的数学平均分,以及各自人数,要求:分数低于70分的不参与分组,分组之后人数大于2个的(分组之后进行数据的筛选)
select sex,avg(math) from stu where math >70 group by sex having count(*) >2;
6.注意:分组之后,查询字段为聚合函数和分组字段,查询其他字段无任何意义
九、分页查询
1.语法:SELECT 字段列表 FROM 表名 LIMIT 起始索引, 查询记录数;
2.起始索引:从0开始
3.起始索引 = (当前页码 -1) * 每页显示的条数
4.从0 开始查询,查询3条数据
select * from stu limit 0,3;
5.每页显示3条数据,查询第1页数据
select * from stu limit 0,3;
6.每页显示3条数据,查询第2页数据
select * from stu limit 3,3;
7.每页显示3条数据,查询第3页数据
select * from stu limit 6,3;