删除 delete from 表名 (where=当)
delete from test where id=12; ==》删除id为12的数据 ;
delete from test where id>9; ==》删除id>9以上的数据 ;
备注:软删除,若设置了自增长,删除后,数据还保留着,若添加数据,还会在最大值上+1(1、2、3三条数据,若设置了自增长,删除第3条数据,再添加数据,此数据id为4)
delete from student; ==> 删除全表,只删表内容,保留表结构;
truncate 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';
update test set name='xiaoxiao' where id>=4; ==> 把id大于等于4的name字段对应的值改为'xiaoxiao';
【面试】一般使用数据库是操作什么场景?
我上家公司使用数据库都是用来查询,只有查询的权限,数据库使用最多的就是查询,其次就是
查询
select * from student; ==> 查询所有数据,*代表所有
select name from student; ==> 查询单个字段
select name,math from student; ==> 查询多个字段
select id,name,math from student where id<=5; ==> 查询多个字段加限制条件
select * from student where sex != 1; ==> 查询sex不等于1的所有数据(方法1)
select * from student where sex <> 1; ==> 查询sex不等于1的所有数据(方法2)
备注:题目当中已知的条件放在where后面,当作条件使用,需要查询的内容放在select后面
select * from student where class=1833 and sex=1; ==> 查询class为1833并且sex为1的所有数据(满足两个条件)
备注:and,此sql必须两个条件都满足才会有结果
真=1,假=0
1&&1 ==》1 1&&0==〉0 0&&1==》0 0&&0==〉0
select * from student where class=1833 or class=1835; ==> 查询class为1833或者class为1835的所有数据(满足两个条件中的一个即可)(方法1)
select * from student where class in(1833,1835); ==> 查询class为1833或者class为1835的所有数据(满足两个条件中的一个即可)(方法2)
select * from student where class not in(1833,1835); ==> 查询class不在1833或1835的所有数据(满足两个条件中的一个即可)(方法2)
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之间的所有数据(包含24和31)
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代表下标,从索引0开始,取5条数据)
select * from student where name like 'xi%'; ==> 查询字段name的值以xi开头的所有数据(模糊匹配,%是通配符)
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的值升序排列的所有数据
【面试题】聚合函数
count() 统计
sum()求和
avg()求平均值
max()求最大值
min()求最小值
distinct()去重
需求:
(1)根据class字段分组,求出每个班级的人数
select class,count(*) from student group by class;
根据class字段分组,求出每个班级的人数,将class取别名为a,count(*)取别名为b
select class a,count(*) b from student group by class;
根据class字段分组,求出每个班级的人数,将class取别名为班级,count(*)取别名为每个班级人数
select class as "班级",count(*) as "每个班级人数" from student group by class;
(2) 通过聚合函数,min()求最小,找出student表中分数最低的
select min(math) from student;
(3) 通过聚合函数,找出student表中分数最高的
select max(math) from student;
(4) 通过聚合函数,求出student表中分数的总和
select sum(math) from student;
(5) 通过聚合函数,求出student表中分数的平均值
select avg(math) from student;
(6) 通过聚合函数,统计表中数据一共多少行
select count(*) from student;
(7)通过聚合函数,把student表中math数据去重
select distinct(math) from student;
(8)求出年龄最大的这个人的姓名和年龄
select name,age from student where age=(select max(age) from student);
select name,age from student order by age desc limit 1;(降序排列取第一个值)
需求:
(1)求出每个班级中数学成绩大于80分的人数
select class,count(math) 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; (给sum(math)取别名为s,后面sum(math)直接可以用s代替)
(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) 向表中插入student中指定的字段id,age,sex; ==> insert into student2(id,age,sex) select id,age,sex from student;
|
|