11.查询字段值为空的数据 select * from +表名 where 字段 is null
注意:字段是空不能写成 字段=null
select * from stu where class is null; ==》查询班级为空的数
12.查询字段不为空的数据 select * from +表名 where 字段 is not null
select * from stu where class is not null; ==》查询班级部位空的数据
【注意:0 不等于null 0是一个值, null 是一个空属性】
13.查询某个字段模糊匹配成功的数据 select * from +表名 where 字段 like “%值%”
%用于匹配字段开头和结尾
select * from stu where name like "xi%"; ==》查询表中字段name以xi字符开头的数据
select * from stu where name like "%li%"; ==》查询表中字段name包含li字符的数据
select * from stu where name like "%li"; ==》表中字段name以li字符结尾的数据
14.查询限定的数量的数据 select * from +表名 where 字段 limit m,n m 指下标,
n指限定的数量,下标为m的开始的n条数据
查询前1-5行的数据:select * from stu limit 0,5 ==>前面的数字1-1=0, 后面的数字0+?=5 ==0+5=5
查询3-8行数据: select * from stu limit 2,6;
查询4-7行的数据:select * from stu limit 3,4;
15.查询的数据根据某个字段从小到大排序 select * from +表名 order by 字段
asc order by ...asc 从小到大排序
select * from stu order by math asc; ==》查询表中数据以数学字段升序排序
==》求每个班的数学总成绩大于200的班级和成绩信息
16.查询的数据根据某个字段从大到小排序 select * from +表名 order by
字段 desc order by ... desc 从大到小排序
select * from stu order by math desc; ==》查询表中数据以数学字段降序排序
17.查询的数据根据某个字段进行分组 select * from +表名 group by 字段
group by ... 根据条件分组
select count(*),class from stu group by class; ==》统计每个班的人数
select count(*) as 每个班的人数,class 班级 from stu group by class;
==》统计每个班的人数,且count(*) 取个别名为每个班的人数 class 取个别名为班级
18.查询的数据根据某个字段进行分组再条件过滤 select * from +表名
group by 字段 having 条件 having跟在group by 后面,作用相当于where
select sum(math),class from stu group by calss having sum(math)>200;
mysql聚合函数
19. 统计查询数据的数量 select count(*) from +表名
select count(*),class from stu group by class; ==》统计每个班的人数
20. 查询某个字段求和 select sum(字段) from +表名
select sum(math),class from stu group by class; ==》统计每个班的数学总分
21. 查询某个字段进行平均值 select avg(字段) from +表名
select avg(math),class from stu group by class; ==》统计每个班的数学平均分
22. 查询某个字段最大值 select max(字段) from +表名
select max(math),class from stu group by class; ==》统计每个班的最高分
23. 查询某个字段最小值 select min(字段) from +表名
select min(math),class from stu group by class; ==》统计每个班的最低分
24 对某个字段进行去重 select distinct(字段) from +表名
select distinct(sex) from stu; ==》按性别去重
题目:
1)求出每个班中数学成绩大于80分的人数
select count(*),class from stu where math >80 group by class;
2)求出每个班中性别为1的数学总成绩
select sum(*),class from stu where sex = 1 group by class;
3)求每个班的数学总成绩大于200的班级和成绩信息
select sum(math)s,class from stu group by class having s>200;
【总结】
1)分组函数group by 只能和聚合函数一起使用、还有分组的字段一起使用
2)where 后面可以接group by 分组函数 、但是group by 分组函数后面不能接where
3)group by 前面加where 是为了先过滤后分组
4)hving 可以放在group by 后面,先分组再过滤 类似where
11、数据库的备份
1)备份表
create table dcs1 like dcs; ==》创建一个dcs1表和dcs表一样的表结构
insert into dcs1 select * from dcs; ==》往dcs1表中插入dcs表中所有的数据
insert into dcs1(id,name,phone)select id,name,phone from dcs;
==》往dcs1表中插入和dcs表中id name phone字段一样的数据
2)备份库(在Linux中操作)
mysqldump -uroot -p123456 dcs9 >/dcs5/dcs9.sql ==》把dcs9这个库备份到根目录下dcs5下命名为dcs9.sql文件
mysql -uroot -p123456 dcs9 </dcs5/dcs9.sql ==》把根目录下dcs5下dcs9.sql 文件还原到dcs9这个库中
12、数据库权限
1)use mysql; ==>进入mysql数据库
2)select host,user from user; ==》查看mysql数据库有哪些用户
localhost和127.0.0.1代表的是本地用户, %号代表的是具有远程权限的用户
3)insert into user(host,user,password)values('locaohost','duan',password("123456"));
==》插入新用户duan,不具有权限
4)flush privileges; ==》刷新权限
5)show grants for 'duan'@'localhost'; ==》查看duan这个用户有哪些权限
GRANT SELECT, UPDATE, DELETE, DROP ON *.* TO 'duan'@'localhost' IDENTIFIED BY PASSWORD
'*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' | ==》新用户duan,不具有权限
6)grant select,uodate,delete,drop on *.* to 'duan'@'localhost' identified by "123456";
==》给duan这个用户赋予查询 更新 删除的权限
7)flush privileges; ==》刷新权限
8)show grants for 'duan'@'localhost'; ==》查看duan这个用户有哪些权限
GRANT SELECT, UPDATE, DELETE, DROP ON *.* TO 'duan'@'localhost' IDENTIFIED BY PASSWORD
'*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' ==》说明有权限
9)grant all privileges on *.* to 'root'@'%' identified by "123456";
==》给远程用户root赋予所有权限
10)flush privileges ==》刷新权限
11)revoke all on *.* from 'duan'@'localhost' ==》移除本地用户duan所有的权限
flush privileges ==》刷新权限
show grants for 'duan'@'localhost'; ==》查看duan这个用户有哪些权限
12) update user set pawssword = password('654321') where user = 'duan';
===》修改duan用户的密码为654321
13)delete from user where user = 'duan'; ==》删除duan这个用户
|
|