Mysql DBA 高級運維學習筆記-DQL語句之select知識講解
阿新 • • 發佈:2018-01-27
mar 指定 dba column exist 官方 his 查看表 表達式 9.9.8 查詢數據
9.9.8.1 查詢表的所有數據行
(1)命令語法:select<字段1,字段2,…>from<表名>where<表達式>
(2)列子:查看表wwn中所有數據
a.進入指定數據庫查詢
system@ceshi 04:5152->use wwn Database changed system@ceshi 04:5159->select * from test -> ; +----+-----------+ | id | name | +----+-----------+ | 1 | wwnwan| | 2 | zbf | | 3 | lisi | | 4 | woshishei | | 5 | nimei | +----+-----------+
b.直接查詢庫下面的表的數據;
system@ceshi 04:5311->use zbf Database changed system@ceshi 04:5915->select id from test; ERROR 1146 (42S02): Table ‘zbf.test‘ doesn‘t exist system@ceshi 04:5919->select * from wwn.test;?用點號分隔庫和表 +----+-----------+ | id | name | +----+-----------+ | 1 | wwnwan| | 2 | zbf | | 3 | lisi | | 4 | woshishei | | 5 | nimei | +----+-----------+
c.推薦指定字段查詢
system@ceshi 04:5938->use wwn;
Database changed
system@ceshi 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行數據
執行命令:
system@ceshi 05:0156->select id,name from test limit 2;
+----+--------+
| id | name |
+----+--------+
| 1 | wwnwan |
| 2 | zbf|
+----+--------+
(2)根據固定條件查詢數據
執行命令:
system@ceshi 05:0500->select * from test where id=1;
+----+--------+
| id | name |
+----+--------+
| 1 | wwnwan |
+----+--------+
1 row in set (0.00 sec)
查詢字符要加引號
system@ceshi 05:1036->select id,name from test where name=wwnwan;
ERROR 1054 (42S22): Unknown column ‘wwnwan‘ in ‘where clause‘
system@ceshi 05:1752->select id,name from test where name=‘wwnwan‘;
+----+--------+
| id | name |
+----+--------+
| 1 | wwnwan |
多個條件用and查詢,查詢條件既要滿足id=2又要滿足name=zbf;用or查詢,查詢條件都滿足。
system@ceshi 05:2540->select id,name from test where id=2 and name=‘zbf‘;
+----+------+
| id | name |
+----+------+
| 2 | zbf |
+----+------+
(3)指定固定條件範圍查詢
執行命令:
多個條件and取交集
system@ceshi 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取並集
system@ceshi 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.排序功能
升序:
system@ceshi 05:3733->select id,name from test order by id asc;
+----+-----------+
| id | name |
+----+-----------+
| 1 | wwnwan|
| 2 | zbf |
| 3 | lisi |
| 4 | woshishei |
| 5 | nimei |
+----+-----------+
降序:
system@ceshi 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.學生表插入數據
system@ceshi 07:0129->insert into student values(0001,‘張三‘,‘男‘,‘22‘,‘計算機網絡‘);
Query OK, 1 row affected, 1 warning (0.00 sec)
system@ceshi 07:0616->insert into student values(0002,‘李四‘,‘男‘,‘21‘,‘計算機網絡‘);
Query OK, 1 row affected, 1 warning (0.00 sec)
system@ceshi 07:0625->insert into student values(0003,‘王二‘,‘男‘,‘28‘,‘物流管理‘);
Query OK, 1 row affected, 1 warning (0.00 sec)
system@ceshi 07:0844->insert into student values(0004,‘脈動‘,‘男‘,‘29‘,‘computer application‘);
Query OK, 1 row affected, 2 warnings (0.00 sec)
system@ceshi 07:0852->insert into student values(0005,‘woshishei‘,‘女‘,‘26‘,‘計算機科學與技術‘);
Query OK, 1 row affected, 2 warnings (0.00 sec)
system@ceshi 07:0944->insert into student values(0006,‘瑩瑩‘,‘女‘,‘26‘,‘護士‘);
Query OK, 1 row affected, 1 warning (0.00 sec)
2.課程表插入數據
system@ceshi 07:1221->insert into course values(1001,‘linux中高級運維‘,‘3‘);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:1349->insert into course values(1002,‘linux高級架構師‘,‘3‘);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:1410->insert into course values(1003,‘MySQL 高級Dba ‘,‘4‘);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:1507->insert into course values(1004,‘Python 運維開發‘,‘4‘);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:1555->insert into course values(1005,‘Jave web 開發‘,‘3‘);
Query OK, 1 row affected (0.00 sec)
3.選課表插入數據
system@ceshi 07:1618->insert into SC(Sno,Cno,Grade) values(0001,1001,3);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2042->insert into SC(Sno,Cno,Grade) values(0001,1002,3);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2105->insert into SC(Sno,Cno,Grade) values(0001,1003,4);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2114->insert into SC(Sno,Cno,Grade) values(0001,1004,4);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2121->insert into SC(Sno,Cno,Grade) values(0002,1001,3);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2209->insert into SC(Sno,Cno,Grade) values(0002,1002,3);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2215->insert into SC(Sno,Cno,Grade) values(0002,1003,4);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2221->insert into SC(Sno,Cno,Grade) values(0002,1004,4);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2228->insert into SC(Sno,Cno,Grade) values(0003,1001,3);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2318->insert into SC(Sno,Cno,Grade) values(0003,1002,3);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2328->insert into SC(Sno,Cno,Grade) values(0003,1003,4);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2334->insert into SC(Sno,Cno,Grade) values(0003,1004,4);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2339->insert into SC(Sno,Cno,Grade) values(0004,1001,3);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2405->insert into SC(Sno,Cno,Grade) values(0004,1002,3);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2413->insert into SC(Sno,Cno,Grade) values(0004,1003,4);
Query OK, 1 row affected (0.00 sec)
system@ceshi 07:2421->insert into SC(Sno,Cno,Grade) values(0004,1004,4);
Query OK, 1 row affected (0.00 sec)
4.聯合查詢,查詢學生成績
system@ceshi 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 運維開發 |
+-----+--------+-------+----------------------+
也可以對查詢進行排序
system@ceshi 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查詢
system@ceshi 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列創建索引。
system@ceshi 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查詢。
system@ceshi 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知識講解