一. 创建学生表

1.进入MySQL

WIN + R 输入cmd

输入mysql -u root -p

输入密码进入mysql

2.

输入show databases;

查询现有数据库

选择一个数据库

use 数据库名称

创建一个学生表

create table stu(
id int ,– 编号
name varchar(10),– 姓名
gender char(1),– 性别
birthday date,– 生日
score double(5,2) ,– 分数
email varchar(64),– 邮箱
tel varchar(20),– 手机号
status tinyint– 状态
);

二.实现查询功能

1.查询所有数据

select * from stu;

2.给指定列添加数据INSERT INTO 表名(列名1,列名2,…) VALUES(值1,值2,…);

INSERT INTO stu (id, NAME)
VALUES
(1, ‘张三’);

3.修改数据 UPDATE 表名 SET 列名1=值1,列名2=值2,… [WHERE 条件]

— 将马运的性别改成女

update stu set sex = ‘女’ where name = ‘马运’;

–将张三的生日改为 1999-12-12 数学分数改为99.99

updatestu set hire_date = ‘1999-12-12’, math = ‘99.99’ where name = ‘马运’;

— 注意:如果update语句没有加where条件,则会将表中所有数据全部修改!

4.删除数据 DELETE FROM 表名 [WHERE 条件] ;

— 删除张三记录

delete from stu where name = ‘张三’;

4.条件查询

— 1.查询年龄大于20岁的学员信息

select * from stu where age > 20;

— 2.查询年龄大于等于20岁的学员信息

select * from stu where age >=20;

— 3.查询年龄大于等于20岁 并且 年龄 小于等于 30岁 的学员信息

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’ 之间的学员信息

select *from stu where hire_date between ‘1998-09-01′ and’1999-09-01’;

— 5. 查询年龄等于20岁的学员信息

select *from stu where age = 20;

— 6. 查询年龄不等于20岁的学员信息

select *from stu where age!= 20;

select *from stu where age 20;

— 7. 查询年龄等于20岁 或者 年龄等于22岁 或者 年龄等于55岁的学员信息

select *from stu where age = 20 or age = 22 or age =55;

select * from stu where age in (20,22,55);

— 8. 查询英语成绩为 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;

5.模糊查询

— 模糊查询 like =====================
/*
通配符:
(1)_:代表单个任意字符
(2)%:代表任意个数字符
*/

— 1. 查询姓’马’的学员信息

select * from stu where name like ‘马%’;

— 2. 查询第二个字是’花’的学员信息

select * from stu where name like ‘_花%’;

— 3. 查询名字中包含 ‘德’ 的学员信息

select * from stu where name like ‘%德%’;

6.排序查询

排序查询:
* 语法:SELECT 字段列表 FROM 表名 ORDER BY 排序字段名1 [排序方式1],排序字段名2 [排序方式2] …;
* 排序方式:
* ASC:升序排列(默认值)
* DESC:降序排列

— 1.查询学生信息,按照年龄升序排序

select *from stu order by age;

— 2.查询学生信息,按照数学成绩降序排列

select *from stu order by age desc;

— 3.查询学生信息,按照数学成绩降序排列,如果数学成绩一样,再按照英语成绩升序排列

select *from stu order by math desc,english asc;

7.分组函数
SELECT 字段列表 FROM 表名 [WHERE 分组前条件限定] GROUP BY 分组字段名 [HAVING 分组后条件过滤]…;

— 1. 查询男同学和女同学各自的数学平均分

select sex,avg(math) from stu group by sex;

注意:分组之后,查询的字段只能为聚合函数和分组函数,查询其他字段无意义

— 2. 查询男同学和女同学各自的数学平均分,以及各自人数

select sex,avg(math),count(*) from stu group by sex;

— 3. 查询男同学和女同学各自的数学平均分,以及各自人数,要求:数学分数低于70分的不参与分组

select sex,avg(math),count(*) from stu where math > 70 group by sex ;

— 4. 查询男同学和女同学各自的数学平均分,以及各自人数,要求:数学分数低于70分的不参与分组,分组之后人数大于1个的。

select sex,avg(math),count(*) from stu where math > 70 group by sex having count(*) > 1;

8.分页查询:

SELECT 字段列表 FROM 表名 LIMIT 起始索引 , 查询条目数
* 起始索引:从0开始

— 1. 从0开始查询,查询3条数据

select * from stu limit 0,3;

— 2. 每页显示3条数据,查询第1页数据

select * from stu limit 0,3;

— 3. 每页显示3条数据,查询第2页数据

select * from stu limit 3,3;

— 4. 每页显示3条数据,查询第3页数据

select * from stu limit 6,3;

— 起始索引 = (当前页码 – 1) * 每页显示的条数