找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
本帖最后由 南京1期-孙兆圣 于 2021-12-1 21:48 编辑

MySQL 第二天
一、删除命令用法
delete from test where id =12; ==》删除id为12的数据
delete from test where id>9; ==》删除id大于9的数据
备注:软删除,若设置了自增长,删除后,数据还保留着,你若添加数据,还是会在最大值上+1
delete from student; ==》删除表数据,表名为student(只删除表内容,保留表结构)
truncate student; ==》删除表数据,表名为student(只删除表内容,保留表结构)
drop table student; ==》删除了表数据和表结构(删除了整个表,删除后就消失了)
二、修改命令用法
update test set score=100 where id=8; ==》把id为8的score字段对应的值改为100
update test set name='xiaowang' where id>5; ==》把id大于5的name字段对应的值修改为xiaowang

【面试题】
你一般使用数据库是操作什么场景?
答:我在上家公司使用数据库都是用来查询的,只有查询的权限(数据库使用最多的就是查询,其次是添加、修改数据,基本上没用过删除)

三、查询命令用法
select * from student; ==》查询所有数据,“ * ”代表所有
select name from student; ==》查询单个字段(查询name字段)
select name,math from student; ==》查询两个字段(name和math字段)
————————————————————————————————————————
不等于查询方法1:
select * from student where sex !=1; ==》查询sex不等于1的所有数据(“ ! ”表示不等于)
不等于查询方法2:
select * from student where sex <> 1; ==》查询sex不等于1的所有数据(“ <> ”也表示不等于)
————————————————————————————————————————
select id,name,math from student where id<=5; ==》查询id小于等于5的信息,并且字段显示为id,name,math
备注:题目当中已知的条件,一定是放在where后面,当做条件使用,需要查询的内容放在select后面
select * from student where class=1833 and sex=1; ==》查询班级class为1833的并且sex为1的所有数据
select * from student where class=1833 or class=1835; ==》查询班级class为1833或者班级class为1835的所有数据
备注:and,表示此sql必须两个条件都满足才会有结果;or ,表示此sql两个条件都满足或者任意一个条件满足都有结果;
select * from student where class in(1833,1835); ==》查询班级为1833或班级为1835的所有数据
select * from student where class not in(1833,1835); ==》查询班级不在1833或1835的所有数据
select * from student where age>24 and age<31; ==》查询年龄age大于24并且小于31的所有数据
select * from student where age>=24 and age<=31; ==》查询年龄age大于等于24并且小于等于31的所有数据
select * from student where age between 24 and 31; ==》表示查询年龄age在24和31之间的所有数据(不在范围内,在between前加not)
select * from student where class is null; ==》查询班级为空的数据
select * from student where class is not null; ==》查询班级不为空的数据
备注:0不等于null,null指的是一个空的属性,0表示一个值;null的前面只能用“ is ”
select * from student limit 5; ==》查询前5行的数据
select * from student limit 0,5; ==》查询前5行数据(0代表是第一行下标,第二行下标为1)
select * from student limit 2,6; ==》查询3到8行的数据(2为下标“3-1”,6为8-2为6)
select * from student where name like 'xi%'; ==》查询name字段开头为xi的数据(mysql中模糊匹配用通配符“ % ”)
select * from student where name like '%qi'; ==》查询name字段结尾为qi的数据
select * from student where name like '%ao%'; ==》查询name字段中包含ao的数据
【面试题】
select * from student order by math desc; ==》将math字段进行降序排序
select * from student order by math asc; ==》将math字段进行升序排序  
备注:desc:descend 下降;asc:ascend 上升;

四、聚合函数用法
count():统计
sum():求和
avg():求平均值
max():求最大值
min():求最小
distinct():去重

1)根据班级class字段分组,人后求出每个班级的人数
select class,count(*) from student group by class;
2)帮class,count(*)取别名a和b,在结果页面显示
select class a,count(*) b from student group by class;
3)帮class,count(*)取中文别名“班级”和“每个班级人数”,在结果页面显示
select class as "班级", count(*) as "每个班级人数" from student group by class;
4)求出表中math字段分数最低的数据
select min(math) from student;
5)求出表中math字段分数最高的数据
select max(math) from student;
6)求出表中math字段的分数总和
select sum(math) from student;
7)求出表中math字段的平均值
select avg(math) from student;
8)统计student表中有多少行
select count(*) from student;
9)把student表中分数重复的去重;
select distinct(math) from student;
10)查询姓名和年龄,找出年龄最大的数据
select age,name from student where age=(select max(age) from student);
select age,name from student order by age desc limit 1; 通过降序从大到小进行排列,再通过limit取第一个值


1)求出每个班级中数学成绩大于80分的人数
select class,count(*) from student where math>80 group by class;
2)求出每个班级中性别为1的数学总成绩
select class,sum(math) from student where sex=1 group by class;
3)求出每个班级数学总成绩大于200的班级和成绩信息
select class,sum(math) from student group by class having sum(math)>200;
select class,sum(math)s from student group by class having s>200; 取别名代
4)查询每个班级数学成绩最高的分数
select class,max(math) from student group by class;
5)查询每个班级数学成绩最低的分数
select class,min(math) from student group by class;

【重点】
1、分组函数 group by 只能和聚合函数和分组字段一起使用;
2、where 的后面可以接 group by 分组函数,但是 group by 分组函数不能跟 where ;
3、group by 分组函数前面加 where 是为了先过滤,再分组
4、having 跟在 group by 后面,作用相当于 where

【备份表】
1)创建一个student1表,表结构和student表一致
create table student1 like student;
2)往student1这个表中插入student表中所有数据
insert into student1 select * from student;
3)创建一个student2,表结构和student表一致
create table student2 like student;
4)往student2表中插入student表中的id,sex,age字段对应的数据
insert into student2(id,age,sex) select id,age,sex from student;

分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册