Mysql DBA 高级运维学习笔记-DQL语句之select知识讲解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql DBA 高级运维学习笔记-DQL语句之select知识讲解相关的知识,希望对你有一定的参考价值。

9.9.8 查询数据

9.9.8.1 查询表的所有数据行

(1)命令语法:select<字段1,字段2,…>from<表名>where<表达式>

(2)列子:查看表wwn中所有数据

a.进入指定数据库查询

[email protected] 04:5152->use wwn
Database changed
[email protected] 04:5159->select * from test
-> ;
+----+-----------+
| id | name  |
+----+-----------+
|  1 | wwnwan|
|  2 | zbf   |
|  3 | lisi  |
|  4 | woshishei |
|  5 | nimei |
+----+-----------+

b.直接查询库下面的表的数据;

[email protected] 04:5311->use zbf
Database changed
[email protected] 04:5915->select id from test;
ERROR 1146 (42S02): Table ‘zbf.test‘ doesn‘t exist
[email protected] 04:5919->select * from wwn.test;?用点号分隔库和表
+----+-----------+
| id | name  |
+----+-----------+
|  1 | wwnwan|
|  2 | zbf   |
|  3 | lisi  |
|  4 | woshishei |
|  5 | nimei |
+----+-----------+

c.推荐指定字段查询

[email protected] 04:5938->use wwn;
Database changed
[email protected] 05:0134->select id,name from test;
+----+-----------+
| id | name  |
+----+-----------+
|  1 | wwnwan|
|  2 | zbf   |
|  3 | lisi  |
|  4 | woshishei |
|  5 | nimei |
+----+-----------+

9.9.8.2 根据指定条件查询表的部分数据

(1)列子:查看表test表中前2行数据

执行命令:

[email protected] 05:0156->select id,name from test limit 2;
+----+--------+
| id | name   |
+----+--------+
|  1 | wwnwan |
|  2 | zbf|
+----+--------+

(2)根据固定条件查询数据

执行命令:

[email protected] 05:0500->select * from test where id=1;
+----+--------+
| id | name   |
+----+--------+
|  1 | wwnwan |
+----+--------+
1 row in set (0.00 sec)

查询字符要加引号

[email protected] 05:1036->select id,name from test where name=wwnwan;
ERROR 1054 (42S22): Unknown column ‘wwnwan‘ in ‘where clause‘
[email protected] 05:1752->select id,name from test where name=‘wwnwan‘;
+----+--------+
| id | name   |
+----+--------+
|  1 | wwnwan |

多个条件用and查询,查询条件既要满足id=2又要满足name=zbf;用or查询,查询条件都满足。

[email protected] 05:2540->select id,name from test where id=2 and name=‘zbf‘;
+----+------+
| id | name |
+----+------+
|  2 | zbf  |
+----+------+

(3)指定固定条件范围查询

执行命令:

多个条件and取交集

[email protected] 05:3022->select id,name from test where id>2 and id<4;
+----+------+
| id | name |
+----+------+
|  3 | lisi |
+----+------+
1 row in set (0.00 sec)

多个条件or取并集

[email protected] 05:3344->select id,name from test where id>2 or id<4;
+----+-----------+
| id | name  |
+----+-----------+
|  1 | wwnwan|
|  2 | zbf   |
|  3 | lisi  |
|  4 | woshishei |
|  5 | nimei |

(4)其他查询功能

a.排序功能

升序:

[email protected] 05:3733->select id,name from test order by id asc;
+----+-----------+
| id | name  |
+----+-----------+
|  1 | wwnwan|
|  2 | zbf   |
|  3 | lisi  |
|  4 | woshishei |
|  5 | nimei |
+----+-----------+

降序:

[email protected] 05:3757->select id,name from test order by id desc;
+----+-----------+
| id | name  |
+----+-----------+
|  5 | nimei |
|  4 | woshishei |
|  3 | lisi  |
|  2 | zbf   |
|  1 | wwnwan|
+----+-----------+

9.9.9多表查询

9.9.9.1 建立几个关联表

Create Table: CREATE TABLE `student` (
  `Sno` int(10) NOT NULL COMMENT ‘学号‘,
  `Sname` varchar(16) NOT NULL COMMENT ‘姓名‘,
  `Ssex` char(2) NOT NULL COMMENT ‘性别‘,
  `Sage` tinyint(2) NOT NULL DEFAULT ‘0‘ COMMENT ‘学生年龄‘,
  `Sdept` varchar(16) DEFAULT NULL COMMENT ‘学生所在系别‘,
  PRIMARY KEY (`Sno`),
  KEY `index_Sname` (`Sname`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

Create Table: CREATE TABLE `course` (
  `Cno` int(10) NOT NULL COMMENT ‘课程表‘,
  `Cname` varchar(64) NOT NULL COMMENT ‘课程名‘,
  `Ccredit` tinyint(2) NOT NULL COMMENT ‘学分‘,
  PRIMARY KEY (`Cno`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

Create Table: CREATE TABLE `SC` (
  `SCid` int(12) NOT NULL AUTO_INCREMENT COMMENT ‘主键‘,
  `Cno` int(10) NOT NULL COMMENT ‘课程号‘,
  `Sno` int(10) NOT NULL COMMENT ‘学号‘,
  `Grade` tinyint(2) NOT NULL COMMENT ‘学生成绩‘,
  PRIMARY KEY (`SCid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

9.9.9.2 往关联表中填充数据

1.学生表插入数据

[email protected] 07:0129->insert into student values(0001,‘张三‘,‘男‘,‘22‘,‘计算机网络‘);
Query OK, 1 row affected, 1 warning (0.00 sec)
[email protected] 07:0616->insert into student values(0002,‘李四‘,‘男‘,‘21‘,‘计算机网络‘);
Query OK, 1 row affected, 1 warning (0.00 sec)
[email protected] 07:0625->insert into student values(0003,‘王二‘,‘男‘,‘28‘,‘物流管理‘);
Query OK, 1 row affected, 1 warning (0.00 sec)
[email protected] 07:0844->insert into student values(0004,‘脉动‘,‘男‘,‘29‘,‘computer application‘);
Query OK, 1 row affected, 2 warnings (0.00 sec)

[email protected] 07:0852->insert into student values(0005,‘woshishei‘,‘女‘,‘26‘,‘计算机科学与技术‘);
Query OK, 1 row affected, 2 warnings (0.00 sec)

[email protected] 07:0944->insert into student values(0006,‘莹莹‘,‘女‘,‘26‘,‘护士‘);
Query OK, 1 row affected, 1 warning (0.00 sec)

2.课程表插入数据

[email protected] 07:1221->insert into course values(1001,‘linux中高级运维‘,‘3‘);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:1349->insert into course values(1002,‘linux高级架构师‘,‘3‘);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:1410->insert into course values(1003,‘mysql 高级Dba ‘,‘4‘);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:1507->insert into course values(1004,‘Python 运维开发‘,‘4‘);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:1555->insert into course values(1005,‘Jave web 开发‘,‘3‘);
Query OK, 1 row affected (0.00 sec)

3.选课表插入数据

[email protected] 07:1618->insert into SC(Sno,Cno,Grade) values(0001,1001,3);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:2042->insert into SC(Sno,Cno,Grade) values(0001,1002,3);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:2105->insert into SC(Sno,Cno,Grade) values(0001,1003,4);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:2114->insert into SC(Sno,Cno,Grade) values(0001,1004,4);
Query OK, 1 row affected (0.00 sec)
[email protected] 07:2121->insert into SC(Sno,Cno,Grade) values(0002,1001,3);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:2209->insert into SC(Sno,Cno,Grade) values(0002,1002,3);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:2215->insert into SC(Sno,Cno,Grade) values(0002,1003,4);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:2221->insert into SC(Sno,Cno,Grade) values(0002,1004,4);
Query OK, 1 row affected (0.00 sec)
[email protected] 07:2228->insert into SC(Sno,Cno,Grade) values(0003,1001,3);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:2318->insert into SC(Sno,Cno,Grade) values(0003,1002,3);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:2328->insert into SC(Sno,Cno,Grade) values(0003,1003,4);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:2334->insert into SC(Sno,Cno,Grade) values(0003,1004,4);
Query OK, 1 row affected (0.00 sec)
[email protected] 07:2339->insert into SC(Sno,Cno,Grade) values(0004,1001,3);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:2405->insert into SC(Sno,Cno,Grade) values(0004,1002,3);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:2413->insert into SC(Sno,Cno,Grade) values(0004,1003,4);
Query OK, 1 row affected (0.00 sec)

[email protected] 07:2421->insert into SC(Sno,Cno,Grade) values(0004,1004,4);
Query OK, 1 row affected (0.00 sec)

4.联合查询,查询学生成绩

[email protected] 12:5043->select student.Sno,student.Sname,SC.Grade,course.Cname from student,course,SC where student.Sno=SC.Sno and course.Cno=SC.Cno;
+-----+--------+-------+----------------------+
| Sno | Sname  | Grade | Cname|
+-----+--------+-------+----------------------+
|   1 | 张三 | 3 | linux中高级运维 |
|   2 | 李四 | 3 | linux中高级运维 |
|   3 | 王二 | 3 | linux中高级运维 |
|   4 | 脉动 | 3 | linux中高级运维 |
|   1 | 张三 | 3 | linux高级架构师 |
|   2 | 李四 | 3 | linux高级架构师 |
|   3 | 王二 | 3 | linux高级架构师 |
|   4 | 脉动 | 3 | linux高级架构师 |
|   1 | 张三 | 4 | MySQL 高级Dba  |
|   2 | 李四 | 4 | MySQL 高级Dba  |
|   3 | 王二 | 4 | MySQL 高级Dba  |
|   4 | 脉动 | 4 | MySQL 高级Dba  |
|   1 | 张三 | 4 | Python 运维开发  |
|   2 | 李四 | 4 | Python 运维开发  |
|   3 | 王二 | 4 | Python 运维开发  |
|   4 | 脉动 | 4 | Python 运维开发  |
+-----+--------+-------+----------------------+

也可以对查询进行排序

[email protected] 12:5651->select student.Sno,student.Sname,SC.Grade,course.Cname from student,course,SC where student.Sno=SC.Sno and course.Cno=SC.Cno order by Sno;
+-----+--------+-------+----------------------+
| Sno | Sname  | Grade | Cname|
+-----+--------+-------+----------------------+
|   1 | 张三 | 3 | linux中高级运维 |
|   1 | 张三 | 4 | MySQL 高级Dba  |
|   1 | 张三 | 3 | linux高级架构师 |
|   1 | 张三 | 4 | Python 运维开发  |
|   2 | 李四 | 3 | linux高级架构师 |
|   2 | 李四 | 4 | Python 运维开发  |
|   2 | 李四 | 3 | linux中高级运维 |
|   2 | 李四 | 4 | MySQL 高级Dba  |
|   3 | 王二 | 3 | linux中高级运维 |
|   3 | 王二 | 4 | MySQL 高级Dba  |
|   3 | 王二 | 3 | linux高级架构师 |
|   3 | 王二 | 4 | Python 运维开发  |
|   4 | 脉动 | 3 | linux高级架构师 |
|   4 | 脉动 | 4 | Python 运维开发  |
|   4 | 脉动 | 3 | linux中高级运维 |
|   4 | 脉动 | 4 | MySQL 高级Dba  |
+-----+--------+-------+----------------------+

9.9.10 使用explain查询select查询语句获取执行查询计划信息

判断索引

a.表中没有索引,用explain查询select查询

[email protected] 02:1846->explain select * from test where name=‘nimei‘\G
*************************** 1. row ***************************
   id: 1
  select_type: SIMPLE
table: test
 type: ALL
possible_keys: NULL
  key: NULL
  key_len: NULL
  ref: NULL
 rows: 5  查询扫描的行数,没有索引扫描5次
Extra: Using where
1 row in set (0.00 sec)

b.给test表中的name列创建索引。

[email protected] 02:1859->create index index_name on test(name);
Query OK, 5 rows affected (0.06 sec)
Records: 5  Duplicates: 0  Warnings: 0

c.这次我们给test表中的name列创建了索引,在用explain查询select查询。

[email protected] 02:2502->explain select * from test where name=‘nimei‘\G
*************************** 1. row ***************************
   id: 1
  select_type: SIMPLE
table: test
 type: ref
possible_keys: index_name
  key: index_name 表示查询已经走索引了
  key_len: 20 索引的长度,因为基于整个列创建索引
  ref: const
 rows: 1 查询扫描行数,有索引扫描了一行
Extra: Using where

提示:

Explain语法见官方手册:

官方手册需要掌握的章节5,6,7,8,10,11,13,14,15

以上是关于Mysql DBA 高级运维学习笔记-DQL语句之select知识讲解的主要内容,如果未能解决你的问题,请参考以下文章

Mysql DBA 高级运维学习笔记-DML语句之insert知识讲解

Mysql DBA 高级运维学习笔记-mysql建表语句及表知识

Mysql DBA 高级运维学习笔记-Mysql插入中文乱码问题

Mysql DBA 高级运维学习笔记-DML之修改表中的数据实战

Mysql DBA 高级运维学习笔记-Mysql数据库字符集知识

Mysql DBA高级运维学习笔记-mysql数据库介绍