数据的修改查询

原始数据:

MySQL基础教程下篇 (修改查询)

修改数据:

update 表名称 set 字段名=修改值 [,字段名=修改值,...] [where 条件];

1. 将所有学生的成绩改为60

update student set score=60;

MySQL基础教程下篇 (修改查询)

2. 将id为10的学生的成绩修改为100

update student set score=100 where id=10;

MySQL基础教程下篇 (修改查询)

3. 将id大于10的学生,年龄增加2岁,成绩提高5分

update student set age=age+2,score=score+5 where id>6;

MySQL基础教程下篇 (修改查询)

4. 删除记录

1. 删除所有成绩不及格的学生

delete from student where score<60;

MySQL基础教程下篇 (修改查询)

5.查询记录

1. 查询所有学生的所有字段

select * from student;

MySQL基础教程下篇 (修改查询)

2. 查询所有学生的姓名和成绩

select name,score from student;

MySQL基础教程下篇 (修改查询)

3. 查询所有学生的姓名和成绩,并将查询字段的别名改为汉字

select name as 姓名,score as 成绩 from student;

MySQL基础教程下篇 (修改查询)

4. 查询成绩大于80分的学生姓名与成绩

select name,score from student where score>80;

MySQL基础教程下篇 (修改查询)

限制查询:

查询限定条数:select 字段列表 from 表名称 [where 条件] limit 最多记录数;

从指定偏移量查询限定条数:select 字段列表 from 表名称 [where 条件] limit 偏移量,最多记录数;

1.查询student表中的前3条记录

select * from student limit 3;

MySQL基础教程下篇 (修改查询)

2.查询student表中的第3~第5条记录(从偏移量为2开始查,最多查3条)

select * from student limit 2,4;

MySQL基础教程下篇 (修改查询)

排序查询:

select 字段列表 from 表名称 [where 条件] order by 字段名[desc] [,字段名...];

1. 按照成绩升序排序

select * from student order by score;

MySQL基础教程下篇 (修改查询)

2. 按照成绩降序排序

select * from student order by score desc;

MySQL基础教程下篇 (修改查询)

3. 先按照成绩降序排序,如果成绩相等,则再按照年龄升序排序

select * from student order by score desc,age;

MySQL基础教程下篇 (修改查询)

4. 查询成绩是前三名的学生记录

select * from student order by score desc limit 3;

MySQL基础教程下篇 (修改查询)

分组查询:

select 字段列表 from 表名称 [where 条件] group by 分组字段列表 [having 对分组后的筛选条件];

注:分组查询经常与聚合函数一起使用

注:分组字段应该与查询字段保持一致

1. 创建产品表

create table product(

id int primary key auto_increment,

name varchar(20) not null,

price double not null,

address varchar(20),

type varchar(20)

);

MySQL基础教程下篇 (修改查询)

2. 插入记录

insert into product(name,price,address,type)values('方便面',5,'北京','零食'),('牙膏',12,'西安','日用品'),('麻辣条',2,'黑龙江','零食'),('Mac电脑',2000,'美国','电子'),('洗面奶',35,'西安','日用品'),('锅巴',8,'河南','零食');

 

MySQL基础教程下篇 (修改查询)

MySQL基础教程下篇 (修改查询)

3. 查询同一种类商品(type)的平均价格各为多少

select type,avg(price) from product group by type;

MySQL基础教程下篇 (修改查询)

4. 查询零食组商品的平均价格为多少?

select type,avg(price) from product group by type having type='零食';

MySQL基础教程下篇 (修改查询)

5. 按照type和address进行分组,每组的平均价格是多少?

select type,address,avg(price) from product group by type,address;

MySQL基础教程下篇 (修改查询)

模糊查询:

select 字段列表 from 表名称 where 字段名 like 匹配条件;

模糊查询中的两个通配符:

1. % 代表任意多个任意字符

2. _ 代表任意一个字符

1. 查询名字中包含'张'的学生记录

select * from student where name like '%张%';

MySQL基础教程下篇 (修改查询)

2. 查询姓'张'的学生记录

select * from student where name like '张%';

MySQL基础教程下篇 (修改查询)

3. 查询名字包含三个字,并且第二个字为'张'的学生记录

select * from student where name like '_张_';

MySQL基础教程下篇 (修改查询)

分页查询

已知当前页为currentPage,每页最多显示的记录数为pageSize,则currentPage页显示的表中的记录为:

select 字段列表 from 表名称 [where 条件] limit (currentPage-1)*pageSize,pageSize;

聚合函数:

1. 查询班级的最高分

select max(score) from student;

MySQL基础教程下篇 (修改查询)

2. 查询班级的平均分

select avg(score) from student;

MySQL基础教程下篇 (修改查询)

3. 查询班级的总分

select sum(score) from student;

MySQL基础教程下篇 (修改查询)

4. 查询班级中成绩不为null的记录数

select count(score) from student;

MySQL基础教程下篇 (修改查询)

5. 查询班级中的总记录数

select count(*) from student;

MySQL基础教程下篇 (修改查询)

6. 通过计算的方式查询班级的平均成绩

select sum(score)/count(score) from student;

MySQL基础教程下篇 (修改查询)

 

相关文章: