6.查询同时满足多个条件数据
select * from +表名 where 条件1 and 条件2
and 关键字左右的两个条件必须同时满足
select * from stu where sex = 1 and class = 1833; 查询class为1833并且sex为1的数据
7.查询满足至少1个条件的数据
select * from +表名 where 条件1 or 条件2
or 关键字左右的两个条件至少满足1个,否则返回空
select * from stu where class = 1833 or class = 1834; ==> 查询class为1833或者class为1835的数据
8.查询一个条件范围内的数据
select * from +表名 where 字段 between m and n
between...and ... 指定一个范围
select * from stu where age between 25 and 30; ==》 查询年龄在25和31 之间的数据
select * from stu where age >=25 and age <=30;
9查询字段满足在指定的集合中的数据
select * from +表名 where 字段 in(值1,值2,值3)
select * from stu where class in (1833,1834); ==> 查询class为1833或者class为1835的数据
10查询字段不满足在指定集合中的数据
select * from +表名 where 字段 not in (值1,值2,值3)
select * from stu where class not in(1833,1834); ==> 查询id 不在1833或者1834里面的数据
11.查询字段值为空的数据
select * from +表名 where 字段 is null
注意:字段是空不能写成 字段=null
select * from student where class is null; ==》查询班级为空的数据
12.查询字段不为空的数据
select * from +表名 where 字段 is not null
select * from student where class is not null; ==》 查询class 字段不为空的数据
注:0不等于null,0是一个值, null 是一个空属性,所以用is
13.查询某个字段模糊匹配成功的数据
select * from +表名 where 字段 like “%值%”
%用于匹配字段开头和结尾
select *from student where name like 'ci%'; ==》 查询姓名以“ci”开头的数据
select * from student where name like '%qi%'; ==》查询姓名以‘qi’结尾的数据
select * from student where name like '%qi'; ==》查询姓名含有‘qi’结尾的数据
14.查询限定的数量的数据
select * from +表名 where 字段 limit m,n
m 指下标,n指限定的数量,下标为m的开始的n条数据
select * from stu limit 0,5; ==》显示1-5行的数据 ==》表示怎么写1-1=0, 0+5 = 5
select * from stu limit 4,6; ==》 显示5-10行的数据 ==》表示怎么写5-1=4 4+6 = 10
select * from stu limit 2,5; ==》显示3-7 行的数据 ==》3-1 = 2 2+5 =7
15.查询的数据根据某个字段从小到大排序
select * from +表名 order by 字段 asc
order by ...asc 从小到大排序
select * from student order by id asc; (ascend) ==》查询表数据按id从小到大排序
16.查询的数据根据某个字段从大到小排序
select * from +表名 order by 字段 desc
order by ... desc 从大到小排序
select * from student ORDER BY id desc; (descend) ==》查询表数据按id从大到小排序
17.查询的数据根据某个字段进行分组
select * from +表名 group by 字段
group by ... 根据条件分组
#根据班级class字段分组、然后求出每个班级的人数
#select class,count(*) from stu group by class;
#select class,count(*) from stu group by class;
#帮class和count(*)分别取别名a和b
#select class as a,count(*) as b from stu group by class;
#帮class和count(*)分别取别名"班级"和"每个班级总人数"
#select class as 班级,count(*) as 每个班级总人数 from stu group by class;
mysql聚合函数
count(*) 统计
sum (字段)求和
avg (字段)求平均
max(字段) 最大值
min (字段)最小值
distinct(字段) 去重
select class,sum(math) from stu group by class; 求每个班级的总分
select class,avg(math) from stu group by class; --》求每个班的平均分数
select max(math) from student; -->求所有班级的最高分
select min(math) from student; --》求所有班级的最低分
select DISTINCT(age),class from student;---》每个根据年龄进行去重
18.查询的数据根据某个字段进行分组再条件过滤
select * from +表名 group by 字段 having 条件
having跟在group by 后面,作用相当于where
题目:
1.求出每个班级中数学成绩大于80分的人数 count(*)
select class,count(*) from student where math>80 group by class;
2.求出每个班级中性别为1的数学总成绩 sum(math)
select class,sum(math) from student where sex=1 group by class;
3.求出每个班级的数学总成绩大于200的班级和成绩信息 sum(math)
select class,sum(math) from stu group by class having sum(math)>200;
select class,sum(math)s from stu group by class having s>200;
【重点:(单表查询总结)】
1.分组函数group by只能和聚合函数、还有分组的字段一起使用
2.where后面可以接group by分组函数、但是group by分组函数后面不能接where
3.group by前面加where是为了先过滤再分组,
4.having 可以放在group by 后面筛选满足条件的组(先分组再过滤)--》类似where
【备份 表】
1.备份表,创建一个表与某个表相同
create table +表1 like +表2
#create table stu1 like stu; --》创建一个stu1表,和stu 一样的表结构
2.备份数据,把一个表的数据插入到另一个表
insert into +表名 select * from +表名
注意点:插入的表必须要存在
#insert into stu1 select * from stu;---》往stu1 表中插入stu所有的数据
insert into +表1(字段1,字段2) select 字段1,字段2 from 表2
注意点
1. 插入的表必须存在
2. 插入的表是新表,没有数据。
#insert into stu1(id,name,math) select id,name,math from stu;--》往stu1表中插入stu表中的id,name,math字段对应的数据
(注意这里字段顺序要一致)
【备份库】
mysqldump -uroot -p123456 dcs16>/dcs16_sql ---》把dcs16这个库备份到根目录下面的dcs16_sql文件中
mysql -uroot -p123456 dcs_bak</dcs16_sql --》把根目录中dcs16_sql文件还原到dcs16库中
【数据库权限】
进入mysql : mysql -uroot -p123456
use mysql; --》使用mysql 数据库
select host,user from user; --》查看mysql数据库有那些用户 (localhost和127.0.0.1 代表的是本地用户;% 代表的是具有远程访问权限的用户)
insert into user(host,user,passwrod)values('localhost','dcs',password("123456")); --》插入新用户,不具有权限
show grants for 'dcs16'@'localhost'; --》查看dcs16 用户是否具有权限
执行后:ERROR 1141 (42000): There is no such grant defined for user 'dcs16' on host 'localhost'
flush privileges;---》刷新权限表
show grants for 'dcs'@'localhost';再次查看下
执行后:GRANT USAGE ON *.* TO 'dcs16'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'
grant select,update,delete,drop on *.* to 'dcs16'@'localhost' identified by '123456'; --》 对创建的用户进行授权
flush privileges;---》授权后需要刷新权限
show grants for 'dcs16'@'localhost';---》刷新权限后查看权限
revoke all on *.* from 'dcs16'@'localhost'; --》移除本地用户dcs16的所有权限
grant all privileges on *.* to 'root'@'%' identified by '123456'; --》给远程用户root 授予所有权限
grant all privileges on *.* to 'dcs16'@'% identified by '123456'; --》给远程用户dcs16授予所有权限
select host,user from user;--》查看所有用户
delect from user where user='dcs16' and host = '%'; ---》删除dcs 远程的权限
update user set password =password('654321') where user = 'dcs16' and host = 'localhost'; ---》修改dcs16用户的密码
|
|