建表语句:
-- 删除stu表
drop table if exists stu;
-- 创建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');
目录
查询stu表数据
SELECT * FROM stu
查询列数据
-- SELECT 字段列表 FROM 表名;--------------------查询列数据
查询地址信息
select address from stu;
查询name,age两列
select name , age FROM stu
-- SELECT DISTINCT 字段列表 FROM 表名;------------去除重复记录
select distinct address from stu;
查询姓名、数学成绩、英语成绩。并通过as给math和english起别名(as关键字可以省略)
select name,math as 数学成绩,english as 英文成绩 from stu;
select name,math 数学成绩,english 英文成绩 from stu;
条件查询:
-- SELECT 字段列表 FROM 表名 WHERE 条件列表;-- 条件查询
查询年龄大于等于20岁 并且 年龄 小于等于 30岁 的学员信息
select * from stu where age >= 20 && age <= 30;
select * from stu where age >= 20 and age <= 30;
&& 和 and 都表示并且的意思。建议使用 and 。
也可以使用 between ... and 来实现上面需求
select * from stu where age BETWEEN 20 and 30;
查询年龄等于18岁 或者 年龄等于20岁 或者 年龄等于22岁的学员信息
select * from stu where age = 18 or age = 20 or age = 22;
select * from stu where age in (18,20 ,22);
查询英语成绩为 null的学员信息
null值的比较不能使用 = 或者 != 。需要使用 is 或者 is not
select * from stu where english = null; -- 这个语句是不行的
select * from stu where english is null;
select * from stu where english is not null;
模糊查询:
模糊查询使用like关键字,可以使用通配符进行占位:
(1)_ : 代表单个任意字符(2)% : 代表任意个数字符
查询姓'马'的学员信息
select * from stu where name like '马%';
查询第二个字是'花'的学员信息
select * from stu where name like '_花%';
查询名字中包含 '德' 的学员信息
select * from stu where name like '%德%';
排序查询:
-- SELECT 字段列表 FROM 表名
ORDER BY 排序字段名1 [排序方式1],排序字段名2 [排序方式2] …;
查询学生信息,按照年龄升序排列
select * from stu order by age ;
查询学生信息,按照数学成绩降序排列
select * from stu order by math desc ;
查询学生信息,按照数学成绩降序排列,如果数学成绩一样,再按照英语成绩升序排列
select * from stu order by math desc , english asc ;
聚合函数语法:
-- SELECT 聚合函数名(列名) FROM 表;
注意:null 值不参与所有聚合函数运算
统计班级一共有多少个学生
select count(id) from stu;
select count(english) from stu;
上面语句根据某个字段进行统计,如果该字段某一行的值为null的话,将不会被统计。
所以可以在count(*) 来实现。\* 表示所有字段数据,一行中也不可能所有的数据都为null,
所以建议使用 count(*)
select count(*) from stu;
查询数学成绩的最高分:
select max(math) from stu;
查询数学成绩的最低分:
select min(math) from stu;
查询数学成绩的总分:
select sum(math) from stu;
查询数学成绩的平均分:
select avg(math) from stu;
查询英语成绩的最低分:
select min(english) from stu;
where 和 having 区别:
执行时机不一样:where 是分组之前进行限定,不满足where条件,则不参与分组,而having是分组之后对结果进行过滤。
可判断的条件不一样:where 不能对聚合函数进行判断,having 可以。
分组查询:
-- SELECT 字段列表 FROM 表名 [WHERE 分组前条件限定] GROUP BY 分组字段名 [HAVING 分组后条件过滤];
注意:分组之后,查询的字段为聚合函数和分组字段,查询其他字段无任何意义
查询男同学和女同学各自的数学平均分
select sex, avg(math) from stu group by sex;
注意:分组之后,查询的字段为聚合函数和分组字段,查询其他字段无任何意义
select name, sex, avg(math) from stu group by sex; -- 这里查询name字段就没有任何意义
查询男同学和女同学各自的数学平均分,以及各自人数
select sex, avg(math),count(*) from stu group by sex;
查询男同学和女同学各自的数学平均分,以及各自人数,要求:分数低于70分的不参与分组
select sex, avg(math),count(*) from stu where math > 70 group by sex;
查询男同学和女同学各自的数学平均分,以及各自人数,要求:分数低于70分的不参与分组,分组之后人数大于2个的
select sex, avg(math),count(*) from stu
where math > 70 group by sex having count(*) > 2;
分页查询:
-- SELECT 字段列表 FROM 表名 LIMIT 起始索引 , 查询条目数;
起始索引 = (当前页码 - 1) * 每页显示的条数
从0开始查询,查询3条数据
select * from stu limit 0 , 3;
每页显示3条数据,查询第1页数据
select * from stu limit 0 , 3;
每页显示3条数据,查询第2页数据
select * from stu limit 3 , 3;
每页显示3条数据,查询第3页数据
select * from stu limit 6 , 3;