找回密码
 立即注册
  • 便民服务
  • 关注我们
  • 社区新手
4)查   select from 表名
1.查询表中所有数据 select * from +表名 ”*“ 代表所有
2. 查询某个字段的数据 select 字段 from +表名
3. 查询多个字段的数据 select 字段1,字段2 from +表名
4.查询满足某个条件的所有数据 select * from +表名 where 字段=值 where 后面接满足的条件
5.查询不满足某个条件的所有数据 select * from +表名 where 字段!=value “!=” 代表不等于 ,
也可以用符号 ”<>“ 代表不等于

例子:
select * from tab;  ==》查询全表的数据
select id from tab; ==》只查询id的数据
select id,score,name from tab;==》同时查询id score name 三个字段的数据
select id from tab where score = 100;  ==》查询score等于100的id的数据
select *from tab where id >3;  ==》查询id大于3的数
select *from tab where id !=3; ==》查询id不等于3的数据
select *from tab where id <>3; ==》查询id不等于3 的数据


6.查询同时满足多个条件数据 select * from +表名 where 条件1 and 条件2 and
关键字左右的两个条件必须同时满足

7.查询满足至少1个条件的数据 select * from +表名 where 条件1 or 条件2 or
关键字左右的两个条件至少满足1个,否则返回空

8.查询一个条件范围内的数据 select * from +表名 where 字段 between m and n
between...and ... 指定一个范围

9查询字段满足在指定的集合中的数据 select * from +表名 where 字段 in(值1,值2,值3)

10查询字段不满足在指定集合中的数据 select * from +表名 where 字段 not in (值1,值2,值3)

例子:

#查询class为1833并且sex为1的数据
#select * from student where class = 1833 and sex = 1;

#查询class为1833或者class为1835的数据
#select *from student where class = 1833 or class = 1835;
#select * from student where class in (1833,1835);

#查询id 不在1和3里面的数据
#select * from student where id not in (1,3);

#查询年龄大于24和小于31的数据
#select * from student where age > 24 and age <31;

#查询年龄在25和31 之间的数据
#select * from student where age between 25 and 31;




11.查询字段值为空的数据 select * from +表名 where 字段 is null 注意:
字段是空不能写成 字段=null

12.查询字段不为空的数据 select * from +表名 where 字段 is not null

13.查询某个字段模糊匹配成功的数据 select * from +表名 where 字段 like “%值%”
%用于匹配字段开头和结尾

14.查询限定的数量的数据 select * from +表名 where 字段
limit m,n m 指下标,n指限定的数量,下标为m的开始的n条数据
例子:

#查询班级为空的数据
#select * from student where class is null;

#查询class 字段不为空的数据
#select * from student where class is not null;

注:0不等于null,0是一个值,  null 是一个空属性,所以用is
#查询前5行的数据

#select * from student limit 0,5; (select * from student limit 5;)  --》一般数据库从0开始算起的
#查询第3行到第7行的数据

#select * from student limit 2,5; --》(3-1=2,2+5=7 ==》(2,5)帮助理解)
#查询姓名以“ci”开头的数据
#select *from student where name like 'ci%';

#查询姓名以‘qi’结尾的数据
#select * from student where name like '%qi';



15.查询的数据根据某个字段从小到大排序 select * from +表名 order by 字段
asc order by ...asc 从小到大排序

16.查询的数据根据某个字段从大到小排序 select * from +表名 order by 字段
desc order by ... desc 从大到小排序

例子:
#查询表数据按id从小到大排序(升序)
#select * from student order by id asc;  (ascend)
#查询表数据按id从大到小排序(降序)
#select * from student order by  id desc; (descend)



17.查询的数据根据某个字段进行分组 select * from +表名 group by 字段 group by ...
根据条件分组

18.查询的数据根据某个字段进行分组再条件过滤 select * from +表名 group by
字段 having 条件 having跟在group by 后面,作用相当于where

mysql聚合函数 【重要】
19. 统计查询数据的数量 select count(*) from +表名
20. 查询某个字段求和 select sum(字段) from +表名
21. 查询某个字段进行平均值 select avg(字段) from +表名
22. 查询某个字段最大值 select max(字段) from +表名
23. 查询某个字段最小值 select min(字段) from +表名
24 对某个字段进行去重 select distinct(字段) from +表名

例子:
#根据班级class字段分组、然后求出每个班级的人数
#select class,count(*)  from student group by class;
#帮class和count(*)分别取别名a和b
#select class as a,count(*) as b from  student group by class;
#帮class和count(*)分别取别名"班级"和"每个班级总人数"
#select class as 班级,count(*) as 每个班级总人数 from student group by class;
select class,sum(math) from student group by class; 求每个班级的总分
select class,avg(math) from student group by class; --》求每个班的平均分数
select max(math) from student; -->求所有班级的最高分
select min(math) from student; --》求所有班级的最低分
select distinct(age),class from student;---》根据年龄进行去重


题目:
1、求出每隔班数学成绩大于80的人数
select count(*),class from stu where math>80 group by class;

2、求出每个班性别为1的数学总成绩
select sum(math),class from stu where sex = 1 group by class;

3、求出每个班的数学总成绩大于200的班级和成绩信息
select sum(math),class from stu group by class having sum(math) > 200;


单表查询总结
1、分组函数group by 只能和聚合函数一起使用、要接只能接分组字段
2、where 后面可以接group by 分组函数、但是group by 后面不能接where
3、group by 前面夹where 是为了先过滤 后分组
4、having 可以放在group by 后面 筛选满足条件的组==》类似where



12、[数据库的备份]
1)备份表
create table tab1 like tab;  ==》创建一个tab1表,和tab表一样的结构(只是备份结构)
insert into tab1 select * from tab;  ==》往tab1表中插入和tab表一样的数据(全部数据)
insert into tab1(id,name,phone)select id,name,phone from tab;
==》往tab1 表中插入和tab 表中id name phone 一样的数据
注意点
1. 插入的表必须存在
2. 插入的表是新表,没有数据。


2)备份库
mysqldump -uroot -p123456 dcs7 >/dcs7/dcs7_bak  ==》把dcs7这个库备份到根目录下dcs7下名为dcs7_bak

mysql -uroot -p123456 dcs8 </dcs7/dcs7_bak  ==》把根目录下dcs7下名为dcs7_bak 还原到dcs8这个库里面



13、[数据库权限]
进入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','dcs7',password("123456"));  --》插入新用户,不具有权限

show grants for 'dcs7'@'localhost'; --》查看dcs7 用户是否具有权限
执行后:ERROR 1141 (42000): There is no such grant defined for user 'dcs7' on host 'localhost'
flush privileges;---》刷新权限表
show grants for 'dcs7'@'localhost';再次查看下
执行后:GRANT USAGE ON *.* TO 'dcs7'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'

grant select,update,delete,drop on *.* to 'dcs7'@'localhost' identified by '123456';  --》 对创建的用户进行授权

flush privileges;---》授权后需要刷新权限

show grants for 'dcs7'@'localhost';---》刷新权限后查看权限

分享至 : QQ空间
收藏

0 个回复

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