学习记录008-数据库常用命令two

1 查询表的数据:

【select * from 表名;】

2 查询表里面特定字段的信息:多字段查询以逗号分隔

【select 字段,字段 from gdd_stu;】

3 使用where进行条件查询

【select id,sex from gdd_stu where id!=1;】id不等于1

【select id,sex from gdd_stu where id=1;】id=1

【select id,sex from gdd_stu where id<>1; 】符号:<> 也为为不等于

4 使用where进行多条件查询:多条件以 and 进行分隔

【select * from gdd_stu where id=1 and sex=0;】

5 指定范围进行查询

【select id,sex from gdd_stu where id in (1,2,3,4);】

【select id,sex from gdd_stu where id between 1 and 3;】  多用于数组范围的查询

6 查询表数据的前几条

【select id,sex from gdd_stu limit 条数;】

7 使用limit进行范围的查询

【select id,sex from gdd_stu limit 1,5;】

  1 代表的是第二条数据,为数据的角标;5为往后查询几条数据

8 按照升序的方式进行查看数据:大多数情况下所有的数据基本都是按照升序进行排列的

【select * from gdd_stu order by id;】

【select * from gdd_stu order by id asc;】  这是基本不写的,因为本身就是默认升序显示

9 按照降序的方式进行查看表内数据

【select * from gdd_stu order by id desc;】

10 分别进行字段的升序和降序查看:多字段可同时使用升序或者desc

【select * from gdd_stu order by id,class2 desc;】 字段以逗号分隔

11 分组查询-----场景:假设查询男生一组,女生一组的成绩

【select * from gdd_stu group by sex;】

12 使用聚合进行分组的查询

【select count(*),sex from gdd_stu group by sex;】

  count为显示表里面去重有多少组,后面的sex为分隔的字段,为了显示查看

13 分组和条件查询需要同时进行的话

  第一种,使用where和group by

  【select * from gdd_stu where score>100 group by class2;】  group by要在where的后面,不能先分组在按照条件查询

  第二种,先分组再按照条件查询的话,那就得使用having

  【select * from gdd_stu where group by class2 having score>100;】

14 多条件的查询:既有where条件,又有分组,又有order by

【select * from gdd_stu where id>5 group by class2 having score>100 order by Score;】 先where后分组后order by

15 查询表有多少条数据

【select count(*) from gdd_stu;】

【select count(*) as 条数 from gdd_stu ;】  和上面的运行结果一致,只是会有一个显示“条数”的别名,为了查看

16 聚合函数:

  查询表里面特定字段的平均值

  【select avg(id) from gdd_stu ;】

  查询表,进行字段求和

  【select sum(id) from gdd_stu ;】

  求字段里面的最小值

  【select min(id) from gdd_stu ;】

  求字段里面的最大值

  【select max(id) from gdd_stu ;】

17 子查询:如果一条语句是通过where查询出来的另一张表的结果,那么就叫做子查询

  场景:当前有两张表格,其中一张为学生的信息,即gdd_stu,另外一张表存储学生的成绩,即gdd_score,后者有字段sid存储学员的id号且对应学生表的id,当前只知道在学生表里面的该学员名字,但是具体id是不知道的

  【select score from gdd_score where sid = (select id from gdd_stu where name="xiaohei");】

  场景:背景同上,利用like进行模糊查询在某一范围内的结果

  【select score from gdd_score where in (select id from gdd_stu where name like "李%")】

18 多表查询

select * from nhy_stu,nhy_score where nhy_stu.id=nhy_score.sid;

19 当表的名字过长,可进行表名的别名添加

select * from nhy_stu as a,nhy_score as b where a.id=b.sid;

20 左连接

select * from nhy_stu as a left join nhy as b on a.id = b.sid;】左连接后的检索结果是显示左表的所有数据和右表中满足where 条件的数据

21 右连接

select * from nhy_stu as a right join nhy as b on a.id = b.sid;】检索结果是右表的所有数据和左表中满足where 条件的数据

22 内连接:查询出来的结果为两张表一样的数据

select * from nhy_stu as a inner join nhy as b on a.id = b.sid;

23 在linux环境下进行对数据库的操作

  001 通过在linux环境下操作连接数据库

  【mysql  -uroot -p123456 -h118.24.3.30 -P3306】

  -u后面跟的是用户名;-p后面跟的是对应的用户名的密码;-h后面为想要连接的数据库地址

  -P注意这里是大写,后面跟的是端口号,如果端口号没有被修改过,那么-P内容不必写,如果修改过,那么就写-P3306 即可

 

  002 备份数据库

  【mysqldump -u用户 -p密码 -A > all_data.sql】

  -A为备份所有的数据库;all_data.sql为设置备份的所有数据库里面的数据,表格等所有信息为creat语句 

  【mysqldump -u用户 -p密码 -d jxz > jxz.sql】

  -d后面跟的是数据库名称,即为指定数据库进行备份

 

  003 场景:备份数据的同时删除以前的表的数据

  【mysqldump -uroot -p123456 -h192.168.155.153 -P3306 --add-drop-table --add-drop -A > /tmp/all_data.sql】

 

  004 场景:表里面有很多垃圾数据,进行数据的删除,但是保留表结构

  【mysqldump -uroot -p123456 -h192.168.155.153 -P3306 --no-data -d 表名 > /tmp/all_data.sql】

 

  005 恢复数据库数据

  【mysql -uroot -p123456 jxz < jxz.sql】 

 

  006 linux通配符

  在linux下查看某些文件 以p开头的文件  

  【ls p*】

  在linux下进行查看包含某些字符的文件

  【ls *p*】

 

 

posted on 2019-07-24 22:56  郭等等  阅读(114)  评论(0编辑  收藏  举报

导航