1. 程式人生 > >第八章| 2. MySQL資料庫|資料操作| 許可權管理

第八章| 2. MySQL資料庫|資料操作| 許可權管理

1、資料操作

SQL(結構化查詢語言),可以操作關係型資料庫

通過sql可以建立、修改賬號並控制賬號許可權;  通過sql可以建立、修改資料庫、表;  通過sql可以增刪改查資料;

可以通過SQL語句中的DML語言來實現資料的操作,包括

  1. 使用INSERT實現資料的插入
  2. UPDATE實現資料的更新
  3. 使用DELETE實現資料的刪除
  4. 使用SELECT查詢資料以及。

1.1資料的增刪改查

插入資料INSERT 

1. 插入完整資料(順序插入)
    語法一:
    INSERT INTO 表名(欄位1,欄位2,欄位3…欄位n) VALUES(值1,值2,值3…值n);
    語法二:
    INSERT INTO 表名 VALUES (值1,值2,值3…值n);

2. 指定欄位插入資料 語法: INSERT INTO 表名(欄位1,欄位2,欄位3…) VALUES (值1,值2,值3…); 3. 插入多條記錄 語法: INSERT INTO 表名 VALUES (值1,值2,值3…值n), (值1,值2,值3…值n), (值1,值2,值3…值n); 4. 插入查詢結果 語法: INSERT INTO 表名(欄位1,欄位2,欄位3…欄位n) SELECT (欄位1,欄位2,欄位3…欄位n) FROM 表2 WHERE …; 更新資料UPDATE 語法: UPDATE 表名 SET 欄位1
=值1, 欄位2=值2, WHERE CONDITION; 示例: UPDATE mysql.user SET password=password(‘123’) where user=’root’ and host=’localhost’; 刪除資料DELETE 語法: DELETE FROM 表名 WHERE CONITION; 示例: DELETE FROM mysql.user WHERE password=’’; 練習: 更新MySQL root使用者密碼為mysql123 刪除除從本地登入的root使用者以外的所有使用者
View Code

1.2單表查詢

把表中所有行和列都列舉出來使用“*” 來表示所有的列如select * from student; 

查詢部分行和列需要列舉不同的列名,而查詢部分分行需要where子句進行條件限制,如select studentNo,studentName,address from student where address = '河南新鄉';

在查詢中使用列的別名,AS子句可以用來改變結果集中列的名稱,也可以為組合或計算出的列指定名稱,讓標題列的資訊更易懂。如select studentNo AS 學生編號,studentName as 學生姓名,address as 學生地址 from student where address = ‘河南新鄉’;   還有一種情況是為通過計算、合併得到的新列命名 如select firstName+ ‘.’+lastName as 姓名 from employee

 

單表查詢

select distinct 欄位1,欄位2,欄位3 from 庫.表 
    where 條件
    group by 分組條件
    having 過濾  having執行完之後不是去執行order,而是去執行distinct後邊的欄位,進行去重;distinct執行完之後再執行order by
    order by 排序欄位
    limit n; 最後再執行limit
#########簡單查詢 
mysql> use db5;
Database changed
mysql> desc employee;
+--------------+-----------------------+------+-----+---------+----------------+
| Field        | Type                  | Null | Key | Default | Extra          |
+--------------+-----------------------+------+-----+---------+----------------+
| id           | int(11)               | NO   | PRI | NULL    | auto_increment |
| name         | varchar(20)           | NO   |     | NULL    |                |
| sex          | enum('male','female') | NO   |     | male    |                |
| age          | int(3) unsigned       | NO   |     | 28      |                |
| hire_date    | date                  | NO   |     | NULL    |                |
| post         | varchar(50)           | YES  |     | NULL    |                |
| post_comment | varchar(100)          | YES  |     | NULL    |                |
| salary       | double(15,2)          | YES  |     | NULL    |                |
| office       | int(11)               | YES  |     | NULL    |                |
| depart_id    | int(11)               | YES  |     | NULL    |                |
+--------------+-----------------------+------+-----+---------+----------------+
rows in set (0.16 sec)

mysql> select * from employee;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name       | sex    | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
|  1 | egon       | male   |  18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使                  | NULL         |    7300.33 |    401 |         1 |
|  2 | alex       | male   |  78 | 2015-03-02 | teacher                                 | NULL         | 1000000.31 |    401 |         1 |
|  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher                                 | NULL         |    8300.00 |    401 |         1 |
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                                 | NULL         |    3500.00 |    401 |         1 |
|  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher                                 | NULL         |    2100.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher                                 | NULL         |    9000.00 |    401 |         1 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                                 | NULL         |   30000.00 |    401 |         1 |
|  8 | 成龍       | male   |  48 | 2010-11-11 | teacher                                 | NULL         |   10000.00 |    401 |         1 |
|  9 | 歪歪       | female |  48 | 2015-03-11 | sale                                    | NULL         |    3000.13 |    402 |         2 |
| 10 | 丫丫       | female |  38 | 2010-11-01 | sale                                    | NULL         |    2000.35 |    402 |         2 |
| 11 | 丁丁       | female |  18 | 2011-03-12 | sale                                    | NULL         |    1000.37 |    402 |         2 |
| 12 | 星星       | female |  18 | 2016-05-13 | sale                                    | NULL         |    3000.29 |    402 |         2 |
| 13 | 格格       | female |  28 | 2017-01-27 | sale                                    | NULL         |    4000.33 |    402 |         2 |
| 14 | 張野       | male   |  28 | 2016-03-11 | operation                               | NULL         |   10000.13 |    403 |         3 |
| 15 | 程咬金     | male   |  18 | 1997-03-12 | operation                               | NULL         |   20000.00 |    403 |         3 |
| 16 | 程咬銀     | female |  18 | 2013-03-11 | operation                               | NULL         |   19000.00 |    403 |         3 |
| 17 | 程咬銅     | male   |  18 | 2015-04-11 | operation                               | NULL         |   18000.00 |    403 |         3 |
| 18 | 程咬鐵     | female |  18 | 2014-05-12 | operation                               | NULL         |   17000.00 |    403 |         3 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
rows in set (0.00 sec)
View Code
mysql> select id,name,salary from employee;
+----+------------+------------+
| id | name       | salary     |
+----+------------+------------+
|  1 | egon       |    7300.33 |
|  2 | alex       | 1000000.31 |
|  3 | wupeiqi    |    8300.00 |
|  4 | yuanhao    |    3500.00 |
|  5 | liwenzhou  |    2100.00 |
|  6 | jingliyang |    9000.00 |
|  7 | jinxin     |   30000.00 |
|  8 | 成龍       |   10000.00 |
|  9 | 歪歪       |    3000.13 |
| 10 | 丫丫       |    2000.35 |
| 11 | 丁丁       |    1000.37 |
| 12 | 星星       |    3000.29 |
| 13 | 格格       |    4000.33 |
| 14 | 張野       |   10000.13 |
| 15 | 程咬金     |   20000.00 |
| 16 | 程咬銀     |   19000.00 |
| 17 | 程咬銅     |   18000.00 |
| 18 | 程咬鐵     |   17000.00 |
+----+------------+------------+
rows in set (0.00 sec)

mysql> select post from employee;
+-----------------------------------------+
| post                                    |
+-----------------------------------------+
| 老男孩駐沙河辦事處外交大使              |
| teacher                                 |
| teacher                                 |
| teacher                                 |
| teacher                                 |
| teacher                                 |
| teacher                                 |
| teacher                                 |
| sale                                    |
| sale                                    |
| sale                                    |
| sale                                    |
| sale                                    |
| operation                               |
| operation                               |
| operation                               |
| operation                               |
| operation                               |
+-----------------------------------------+
rows in set (0.00 sec)

mysql> select distinct post from employee;  ##distinct是避免重複
+-----------------------------------------+
| post                                    |
+-----------------------------------------+
| 老男孩駐沙河辦事處外交大使                  |
| teacher                                 |
| sale                                    |
| operation                               |
+-----------------------------------------+
rows in set (0.08 sec)

mysql> select name,salary*12 from employee;  ##通過四則運算查詢 
+------------+-------------+
| name       | salary*12   |
+------------+-------------+
| egon       |    87603.96 |
| alex       | 12000003.72 |
| wupeiqi    |    99600.00 |
| yuanhao    |    42000.00 |
| liwenzhou  |    25200.00 |
| jingliyang |   108000.00 |
| jinxin     |   360000.00 |
| 成龍       |   120000.00 |
| 歪歪       |    36001.56 |
| 丫丫       |    24004.20 |
| 丁丁       |    12004.44 |
| 星星       |    36003.48 |
| 格格       |    48003.96 |
| 張野       |   120001.56 |
| 程咬金     |   240000.00 |
| 程咬銀     |   228000.00 |
| 程咬銅     |   216000.00 |
| 程咬鐵     |   204000.00 |
+------------+-------------+
rows in set (0.09 sec)

mysql> select name,salary*12 annual_salary from employee;
+------------+---------------+
| name       | annual_salary |
+------------+---------------+
| egon       |      87603.96 |
| alex       |   12000003.72 |
| wupeiqi    |      99600.00 |
| yuanhao    |      42000.00 |
| liwenzhou  |      25200.00 |
| jingliyang |     108000.00 |
| jinxin     |     360000.00 |
| 成龍       |     120000.00 |
| 歪歪       |      36001.56 |
| 丫丫       |      24004.20 |
| 丁丁       |      12004.44 |
| 星星       |      36003.48 |
| 格格       |      48003.96 |
| 張野       |     120001.56 |
| 程咬金     |     240000.00 |
| 程咬銀     |     228000.00 |
| 程咬銅     |     216000.00 |
| 程咬鐵     |     204000.00 |
+------------+---------------+
rows in set (0.00 sec)

mysql> select concat('姓名:', name, '性別:', sex)as info from employee;  ##concat通常用於連線字串 
+--------------------------------+
| info                           |
+--------------------------------+
| 姓名:egon性別:male             |
| 姓名:alex性別:male             |
| 姓名:wupeiqi性別:male          |
| 姓名:yuanhao性別:male          |
| 姓名:liwenzhou性別:male        |
| 姓名:jingliyang性別:female     |
| 姓名:jinxin性別:male           |
| 姓名:成龍性別:male             |
| 姓名:歪歪性別:female           |
| 姓名:丫丫性別:female           |
| 姓名:丁丁性別:female           |
| 姓名:星星性別:female           |
| 姓名:格格性別:female           |
| 姓名:張野性別:male             |
| 姓名:程咬金性別:male           |
| 姓名:程咬銀性別:female         |
| 姓名:程咬銅性別:male           |
| 姓名:程咬鐵性別:female         |
+--------------------------------+
rows in set (0.14 sec)

mysql> select concat('姓名:', name, '性別:', sex) as info, concat('年薪:', salar
y*12) as annual_salary from employee;
+--------------------------------+--------------------+
| info                           | annual_salary      |
+--------------------------------+--------------------+
| 姓名:egon性別:male             | 年薪:87603.96      |
| 姓名:alex性別:male             | 年薪:12000003.72   |
| 姓名:wupeiqi性別:male          | 年薪:99600.00      |
| 姓名:yuanhao性別:male          | 年薪:42000.00      |
| 姓名:liwenzhou性別:male        | 年薪:25200.00      |
| 姓名:jingliyang性別:female     | 年薪:108000.00     |
| 姓名:jinxin性別:male           | 年薪:360000.00     |
| 姓名:成龍性別:male             | 年薪:120000.00     |
| 姓名:歪歪性別:female           | 年薪:36001.56      |
| 姓名:丫丫性別:female           | 年薪:24004.20      |
| 姓名:丁丁性別:female           | 年薪:12004.44      |
| 姓名:星星性別:female           | 年薪:36003.48      |
| 姓名:格格性別:female           | 年薪:48003.96      |
| 姓名:張野性別:male             | 年薪:120001.56     |
| 姓名:程咬金性別:male           | 年薪:240000.00     |
| 姓名:程咬銀性別:female         | 年薪:228000.00     |
| 姓名:程咬銅性別:male           | 年薪:216000.00     |
| 姓名:程咬鐵性別:female         | 年薪:204000.00     |
+--------------------------------+--------------------+
rows in set (0.00 sec)


mysql> select concat(name, ':', age) from employee;
+------------------------+
| concat(name, ':', age) |
+------------------------+
| egon:18                |
| alex:78                |
| wupeiqi:81             |
| yuanhao:73             |
| liwenzhou:28           |
| jingliyang:18          |
| jinxin:18              |
| 成龍:48                |
| 歪歪:48                |
| 丫丫:38                |
| 丁丁:18                |
| 星星:18                |
| 格格:28                |
| 張野:28                |
| 程咬金:18              |
| 程咬銀:18              |
| 程咬銅:18              |
| 程咬鐵:18              |
+------------------------+
rows in set (0.00 sec)

mysql> select concat_ws(':', name, sex, age) from employee;  ##concat_ws()第一個引數為分隔符
+--------------------------------+
| concat_ws(':', name, sex, age) |
+--------------------------------+
| egon:male:18                   |
| alex:male:78                   |
| wupeiqi:male:81                |
| yuanhao:male:73                |
| liwenzhou:male:28              |
| jingliyang:female:18           |
| jinxin:male:18                 |
| 成龍:male:48                   |
| 歪歪:female:48                 |
| 丫丫:female:38                 |
| 丁丁:female:18                 |
| 星星:female:18                 |
| 格格:female:28                 |
| 張野:male:28                   |
| 程咬金:male:18                 |
| 程咬銀:female:18               |
| 程咬銅:male:18                 |
| 程咬鐵:female:18               |
+--------------------------------+
rows in set (0.00 sec)
View Code
練習:
1 查出所有員工的名字,薪資,格式為 <名字:egon> <薪資:3000> 2 查出所有的崗位(去掉重複) 3 查出所有員工名字,以及他們的年薪,年薪的欄位名為annual_year
mysql> select concat('<名字:', name, '>')from employee;
+--------------------------------+
| concat('<名字>:', name, '>')   |
+--------------------------------+
| <名字:egon>                   |
| <名字:alex>                   |
| <名字:wupeiqi>                |
| <名字:yuanhao>                |
| <名字:liwenzhou>              |
| <名字:jingliyang>             |
| <名字:jinxin>                 |
| <名字:成龍>                   |
| <名字:歪歪>                   |
| <名字:丫丫>                   |
| <名字:丁丁>                   |
| <名字:星星>                   |
| <名字:格格>                   |
| <名字:張野>                   |
| <名字:程咬金>                 |
| <名字:程咬銀>                 |
| <名字:程咬銅>                 |
| <名字:程咬鐵>                 |
+--------------------------------+
rows in set (0.00 sec)

mysql> select concat('<名字:', name, '>'), concat('<薪資:', salary, '>') from employee;
+--------------------------------+---------------------------------+
| concat('<名字>:', name, '>')   | concat('<薪資:', salary, '>')   |
+--------------------------------+---------------------------------+
| <名字:egon>                   | <薪資:7300.33>                  |
| <名字:alex>                   | <薪資:1000000.31>               |
| <名字:wupeiqi>                | <薪資:8300.00>                  |
| <名字:yuanhao>                | <薪資:3500.00>                  |
| <名字:liwenzhou>              | <薪資:2100.00>                  |
| <名字:jingliyang>             | <薪資:9000.00>                  |
| <名字:jinxin>                 | <薪資:30000.00>                 |
| <名字:成龍>                   | <薪資:10000.00>                 |
| <名字:歪歪>                   | <薪資:3000.13>                  |
| <名字:丫丫>                   | <薪資:2000.35>                  |
| <名字:丁丁>                   | <薪資:1000.37>                  |
| <名字:星星>                   | <薪資:3000.29>                  |
| <名字:格格>                   | <薪資:4000.33>                  |
| <名字:張野>                   | <薪資:10000.13>                 |
| <名字:程咬金>                 | <薪資:20000.00>                 |
| <名字:程咬銀>                 | <薪資:19000.00>                 |
| <名字>:程咬銅>                 | <薪資:18000.00>                 |
| <名字:程咬鐵>                 | <薪資:17000.00>                 |
+--------------------------------+---------------------------------+
rows in set (0.00 sec)
View Code
#where約束
select id,name,age from employee where id > 7; #單條件
    
select name,post,salary from employee where post='teacher' and salary > 8000;

select name,salary from employee where salary >= 20000 and salary <= 30000;
select name,salary from employee where salary between 20000 and 30000;

select name,salary from employee where salary < 20000 or salary > 30000;
select name,salary from employee where salary not between 20000 and 30000;


select * from employee where age = 73 or age = 81 or age = 28;
select * from employee where age in (73,81,28);

select * from employee where post_comment is Null; #判斷是否為空
select * from employee where post_comment is not Null;

select * from employee where name like "jin%"; #模糊匹配 %代表任意多個字元,jin開頭的不管後邊是什麼
select * from employee where name like "jin___";  ##___三個下劃線代表3個任意字元

mysql> select id,name,age from employee where id > 7;
+----+-----------+-----+
| id | name      | age |
+----+-----------+-----+
|  8 | 成龍      |  48 |
|  9 | 歪歪      |  48 |
| 10 | 丫丫      |  38 |
| 11 | 丁丁      |  18 |
| 12 | 星星      |  18 |
| 13 | 格格      |  28 |
| 14 | 張野      |  28 |
| 15 | 程咬金    |  18 |
| 16 | 程咬銀    |  18 |
| 17 | 程咬銅    |  18 |
| 18 | 程咬鐵    |  18 |
+----+-----------+-----+
rows in set (0.13 sec)


mysql> select name,post,salary from employee where post='teacher' and salary > 8
000;
+------------+---------+------------+
| name       | post    | salary     |
+------------+---------+------------+
| alex       | teacher | 1000000.31 |
| wupeiqi    | teacher |    8300.00 |
| jingliyang | teacher |    9000.00 |
| jinxin     | teacher |   30000.00 |
| 成龍       | teacher |   10000.00 |
+------------+---------+------------+
rows in set (0.04 sec)

mysql> select name,salary from employee where salary >=20000 and salary <=30000;

+-----------+----------+
| name      | salary   |
+-----------+----------+
| jinxin    | 30000.00 |
| 程咬金    | 20000.00 |
+-----------+----------+
rows in set (0.00 sec)

mysql> select name,salary from employee where salary between 20000 and 30000;
+-----------+----------+
| name      | salary   |
+-----------+----------+
| jinxin    | 30000.00 |
| 程咬金    | 20000.00 |
+-----------+----------+
rows in set (0.07 sec)

mysql> select name,salary from employee where salary < 20000 or salary > 30000;
+------------+------------+
| name       | salary     |
+------------+------------+
| egon       |    7300.33 |
| alex       | 1000000.31 |
| wupeiqi    |    8300.00 |
| yuanhao    |    3500.00 |
| liwenzhou  |    2100.00 |
| jingliyang |    9000.00 |
| 成龍       |   10000.00 |
| 歪歪       |    3000.13 |
| 丫丫       |    2000.35 |
| 丁丁       |    1000.37 |
| 星星       |    3000.29 |
| 格格       |    4000.33 |
| 張野       |   10000.13 |
| 程咬銀     |   19000.00 |
| 程咬銅     |   18000.00 |
| 程咬鐵     |   17000.00 |
+------------+------------+
rows in set (0.01 sec)

mysql> select name,salary from employee where salary not between 20000 and 30000
;
+------------+------------+
| name       | salary     |
+------------+------------+
| egon       |    7300.33 |
| alex       | 1000000.31 |
| wupeiqi    |    8300.00 |
| yuanhao    |    3500.00 |
| liwenzhou  |    2100.00 |
| jingliyang |    9000.00 |
| 成龍       |   10000.00 |
| 歪歪       |    3000.13 |
| 丫丫       |    2000.35 |
| 丁丁       |    1000.37 |
| 星星       |    3000.29 |
| 格格       |    4000.33 |
| 張野       |   10000.13 |
| 程咬銀     |   19000.00 |
| 程咬銅     |   18000.00 |
| 程咬鐵     |   17000.00 |
+------------+------------+
rows in set (0.00 sec)

mysql> select * from employee where age =73 or age = 81 or age = 28;
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| id | name      | sex    | age | hire_date  | post      | post_comment | salary   | office | depart_id |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
|  3 | wupeiqi   | male   |  81 | 2013-03-05 | teacher   | NULL         |  8300.00 |    401 |         1 |
|  4 | yuanhao   | male   |  73 | 2014-07-01 | teacher   | NULL         |  3500.00 |    401 |         1 |
|  5 | liwenzhou | male   |  28 | 2012-11-01 | teacher   | NULL         |  2100.00 |    401 |         1 |
| 13 | 格格      | female |  28 | 2017-01-27 | sale      | NULL         |  4000.33 |    402 |         2 |
| 14 | 張野      | male   |  28 | 2016-03-11 | operation | NULL         | 10000.13 |    403 |         3 |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
rows in set (0.00 sec)

mysql> select * from employee where post_comment is Null;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name       | sex    | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
|  1 | egon       | male   |  18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使              | NULL         |    7300.33 |    401 |         1 |
|  2 | alex       | male   |  78 | 2015-03-02 | teacher                                 | NULL         | 1000000.31 |    401 |         1 |
|  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher                                 | NULL         |    8300.00 |    401 |         1 |
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                                 | NULL         |    3500.00 |    401 |         1 |
|  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher                                 | NULL         |    2100.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher                                 | NULL         |    9000.00 |    401 |         1 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                                 | NULL         |   30000.00 |    401 |         1 |
|  8 | 成龍       | male   |  48 | 2010-11-11 | teacher                                 | NULL         |   10000.00 |    401 |         1 |
|  9 | 歪歪       | female |  48 | 2015-03-11 | sale                                    | NULL         |    3000.13 |    402 |         2 |
| 10 | 丫丫       | female |  38 | 2010-11-01 | sale                                    | NULL         |    2000.35 |    402 |         2 |
| 11 | 丁丁       | female |  18 | 2011-03-12 | sale                                    | NULL         |    1000.37 |    402 |         2 |
| 12 | 星星       | female |  18 | 2016-05-13 | sale                                    | NULL         |    3000.29 |    402 |         2 |
| 13 | 格格       | female |  28 | 2017-01-27 | sale                                    | NULL         |    4000.33 |    402 |         2 |
| 14 | 張野       | male   |  28 | 2016-03-11 | operation                               | NULL         |   10000.13 |    403 |         3 |
| 15 | 程咬金     | male   |  18 | 1997-03-12 | operation                               | NULL         |   20000.00 |    403 |         3 |
| 16 | 程咬銀     | female |  18 | 2013-03-11 | operation                               | NULL         |   19000.00 |    403 |         3 |
| 17 | 程咬銅     | male   |  18 | 2015-04-11 | operation                               | NULL         |   18000.00 |    403 |         3 |
| 18 | 程咬鐵     | female |  18 | 2014-05-12 | operation                               | NULL         |   17000.00 |    403 |         3 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
rows in set (0.00 sec)

mysql> select * from employee where name like "jin%";  ##模糊匹配,%代表任意字元,jin開頭的不管後邊是什麼
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
| id | name       | sex    | age | hire_date  | post    | post_comment | salary| office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         |  9000.00 |    401 |         1 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher | NULL         | 30000.00 |    401 |         1 |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
rows in set (0.09 sec)

mysql> select * from employee where name like "jin___";  ##___三個下劃線代表3個任意字元。 
+----+--------+------+-----+------------+---------+--------------+----------+--------+-----------+
| id | name   | sex  | age | hire_date  | post    | post_comment | salary   | office | depart_id |
+----+--------+------+-----+------------+---------+--------------+----------+--------+-----------+
|  7 | jinxin | male |  18 | 1900-03-01 | teacher | NULL         | 30000.00 |401 |         1 |
+----+--------+------+-----+------------+---------+--------------+----------+--------+-----------+
row in set (0.00 sec)
View Code
group by 分組查詢 

#group by分組 在where之後執行
分組就是按照相應欄位進行歸類,每後邊那個字就是要分的欄位
mysql> set global sql_mode="ONLY_FULL_GROUP_BY"; 分組之後,只能取分組的欄位,以及每個組 聚合的結果 select post from employee group by post;
單獨使用GROUP BY關鍵字分組
    SELECT post FROM employee GROUP BY post;
    注意:我們按照post欄位分組,那麼select查詢的欄位只能是post,想要獲取組內的其他相關資訊,需要藉助函式

GROUP BY關鍵字和GROUP_CONCAT()函式一起使用
    SELECT post,GROUP_CONCAT(name) FROM employee GROUP BY post;#按照崗位分組,並檢視組內成員名
    SELECT post,GROUP_CONCAT(name) as emp_members FROM employee GROUP BY post;

GROUP BY與聚合函式一起使用
    select post,count(id) as count from employee group by post;#按照崗位分組,並檢視每個組有多少人
View Code
#聚合函式 #以組為單位進行統計,不再考慮個人
  max
  min
  avg
  sum
  count

select post,count(id) as emp_count from employee group by post;##每個部門/職位有多少個員工;先確定你的表
select post,max(salary) as emp_max from employee group by post;
select post,min(salary) as emp_min from employee group by post;
select post,avg(salary) as emp_avg from employee group by post;
select post,sum(age) as emp_sum from employee group by post;
mysql> select * from employee group by post;
+----+--------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name   | sex    | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |
+----+--------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 14 | 張野   | male   |  28 | 2016-03-11 | operation                               | NULL         |   10000.13 |    403 |         3 |
|  9 | 歪歪   | female |  48 | 2015-03-11 | sale                                    | NULL         |    3000.13 |    402 |         2 |
|  2 | alex   | male   |  78 | 2015-03-02 | teacher                                 | NULL         | 1000000.31 |    401 |         1 |
|  1 | egon   | male   |  18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使              | NULL         |    7300.33 |    401 |         1 |
+----+--------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
rows in set (0.08 sec)

#由於沒有設定ONLY_FULL_GROUP_BY,於是也可以有結果,預設都是組內的第一條記錄(就是按照post分組,如operation組內的第一條記錄。),但其實這是沒有意義的
mysql> set global sql_mode="ONLY_FULL_GROUP_BY";
Query OK, 0 rows affected (0.11 sec)   ##設定成功一定要先退出,然後重新登入方可生效。然後你再select * 或者 select name就會報錯了。

mysql> exit
Bye

C:\Users\Administrator>mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use db5;
Database changed
mysql> select * from employee group by post;
ERROR 1055 (42000): 'db5.employee.id' isn't in GROUP BY
mysql> select post from employee group by post;
+-----------------------------------------+
| post                                    |
+-----------------------------------------+
| operation                               |
| sale                                    |
| teacher                                 |
| 老男孩駐沙河辦事處外交大使              |
+-----------------------------------------+
rows in set (0.00 sec)

mysql> select post,count(id) as emp_count from employee group by post;  #查詢組內的id數
+-----------------------------------------+-----------+
| post                                    | emp_count |
+-----------------------------------------+-----------+
| operation                               |         5 |
| sale                                    |         5 |
| teacher                                 |         7 |
| 老男孩駐沙河辦事處外交大使                  |         1 |
+-----------------------------------------+-----------+
rows in set (0.06 sec)
View Code
強調:如果我們用unique的欄位作為分組的依據,則每一條記錄自成一組,這種分組沒有意義
多條記錄之間的某個欄位值相同,該欄位通常用來作為分組的依據
#沒有group by則預設整體算作一組
select max(salary) from employee; #取所有員工的最高工資

#group_concat(name)   職位包含的所有員工成員的名字
select post,group_concat(name) from employee group by post;
mysql> select max(salary) from employee;
+-------------+
| max(salary) |
+-------------+
|  1000000.31 |
+-------------+
row in set (0.07 sec)

mysql> select post,group_concat(name) from employee group by post; ##也可以查詢組內成員的性別group_concat(sex);
+-----------------------------------------+-------------------------------------
| post                                    | group_concat(name)
+-----------------------------------------+-------------------------------------
| operation                               | 程咬鐵,程咬銅,程咬銀,程咬金,張野
                    |
| sale                                    | 格格,星星,丁丁,丫丫,歪歪
                    |
| teacher                                 | 成龍,jinxin,jingliyang,liwenzhou,yuanhao,wupeiqi,alex   |

| 老男孩駐沙河辦事處外交大使                  | egon
                    |
+-----------------------------------------+-------------------------------------
rows in set (0.01 sec)
View Code
#練習:
1. 查詢崗位名以及崗位包含的所有員工名字  #先確定你查的是哪張表,有沒有過濾條件where,有沒有分組分類的;
2. 先取出年齡在50以上的然後再 查詢崗位名以及各崗位內包含的員工個數 
3. 查詢公司內男員工和女員工的個數
4. 查詢崗位名以及各崗位的平均薪資
5. 查詢崗位名以及各崗位的最高薪資
6. 查詢崗位名以及各崗位的最低薪資
7. 查詢男員工與男員工的平均薪資,女員工與女員工的平均薪資
mysql> select post,group_concat(name) from  employee group by post;
+-----------------------------------------+------------------------------------
| post                                    | group_concat(name)
+-----------------------------------------+------------------------------------
| operation                               | 程咬鐵,程咬銅,程咬銀,程咬金,張野
                    |
| sale                                    | 格格,星星,丁丁,丫丫,歪歪
                    |
| teacher                                 | 成龍,jinxin,jingliyang,liwenzhou,yunhao,wupeiqi,alex   |

| 老男孩駐沙河辦事處外交大使                  | egon
                    |
+-----------------------------------------+------------------------------------
rows in set (0.00 sec)

mysql> select post,count(id) from employee where age >50 group by post;
+---------+-----------+
| post    | count(id) |
+---------+-----------+
| teacher |         3 |
+---------+-----------+
row in set (0.00 sec)

mysql> select sex,count(id) from employee group by sex;
+--------+-----------+
| sex    | count(id) |
+--------+-----------+
| male   |        10 |
| female |         8 |
+--------+-----------+
rows in set (0.00 sec)

mysql> select sex,avg(salary) from employee group by sex;
+--------+---------------+
| sex    | avg(salary)   |
+--------+---------------+
| male   | 110920.077000 |
| female |   7250.183750 |
+--------+---------------+
rows in set (0.00 sec)

mysql> select sex,max(age) from employee group by sex;
+--------+----------+
| sex    | max(age) |
+--------+----------+
| male   |       81 |
| female |       48 |
+--------+----------+
rows in set (0.06 sec)
View Code
#having 過濾條件 在分組之後進行;where在分組之前  (過濾的是分組組內的成員)

例如:
1. 查詢各崗位內包含的員工個數小於2的崗位名、崗位內包含員工名字、個數  #聚合出員工個數
3. 查詢各崗位平均薪資大於10000的崗位名、平均工資
4. 查詢各崗位平均薪資大於10000且小於20000的崗位名、平均工資  and avg(salary) < 20000;
select post,group_concat(name),count(id) from employee group by post;

select post,group_concat(name),count(id) from employee group by post having count(id) < 2;

select post,avg(salary) from employee group by post having avg(salary) > 10000;
mysql> select post,group_concat(name), count(id) from employee group by post;
+-----------------------------------------+-------------------------------------
| post              |count(id)  |         | group_concat(name)
+-----------------------------------------+-------------------------------------
| operation         |         5 |         | 程咬鐵,程咬銅,程咬銀,程咬金,張野

| sale              |         5 |         | 格格,星星,丁丁,丫丫,歪歪

| teacher           |         7 |         | 成龍,jinxin,jingliyang,liwenzhou,yuanhao,wupeiqi,alex   

| 老男孩駐沙河辦事處外交大使|     1 |         | egon
+-----------------------------------------+-------------------------------------
rows in set (0.00 sec)

mysql> select post,group_concat(name),count(id) from employee group by post having count(id) < 2;
+-----------------------------------------+--------------------+-----------+
| post                                    | group_concat(name) | count(id) |
+-----------------------------------------+--------------------+-----------+
| 老男孩駐沙河辦事處外交大使                  | egon               |         1 |
+-----------------------------------------+--------------------+-----------+
row in set (0.01 sec)

mysql> select post,avg(salary) from employee group by post having avg(salary) >
    -> \c
mysql> select post, avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary) < 20000;
+-----------+--------------+
| post      | avg(salary)  |
+-----------+--------------+
| operation | 16800.026000 |
+-----------+--------------+
row in set (0.00 sec)
View Code
#order by  預設就是升序
select * from employee order by age asc; #升序,不加asc也可以,預設  從低到高
select * from employee order by age desc; #降序                   從高到低

select * from employee order by age asc,id desc; #先按照age升序排,如果age相同則按照id降序排;

聚合函式不能用在where內;分完組之後才能用聚合函式,having可以用;select max(salary) from employee 這裡邊執行max的時候就已經分組了; 先找表,再where再分組,...最後才打印select你想列印的內容
select distinct post,count(id) as emp_count from employee
    where salary > 1000
    group by post
    having count(id) > 1 #在這個位置不能用emp_count>1,因為執行having之前還沒執行distinct這個欄位呢
    order by emp_count desc  #可以用count(id),它是在分組之後乾的;emp_count也可以用,因為order by 是在distinct後邊執行;
mysql> select * from employee order by age asc, id desc;  #在age asc 的組內進行id排序。
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name       | sex    | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 18 | 程咬鐵     | female |  18 | 2014-05-12 | operation                               | NULL         |   17000.00 |    403 |         3 |
| 17 | 程咬銅     | male   |  18 | 2015-04-11 | operation                               | NULL         |   18000.00 |    403 |         3 |
| 16 | 程咬銀     | female |  18 | 2013-03-11 | operation                               | NULL         |   19000.00 |    403 |         3 |
| 15 | 程咬金     | male   |  18 | 1997-03-12 | operation                               | NULL         |   20000.00 |    403 |         3 |
| 12 | 星星       | female |  18 | 2016-05-13 | sale                                    | NULL         |    3000.29 |    402 |         2 |
| 11 | 丁丁       | female |  18 | 2011-03-12 | sale                                    | NULL         |    1000.37 |    402 |         2 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                                 | NULL         |   30000.00 |    401 |         1 |
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher                                 | NULL         |    9000.00 |    401 |         1 |
|  1 | egon       | male   |  18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使              | NULL         |    7300.33 |    401 |         1 |
| 14 | 張野       | male   |  28 | 2016-03-11 | operation                               | NULL         |   10000.13 |    403 |         3 |
| 13 | 格格       | female |  28 | 2017-01-27 | sale                                    | NULL         |    4000.33 |    402 |         2 |
|  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher                                 | NULL         |    2100.00 |    401 |         1 |
| 10 | 丫丫       | female |  38 | 2010-11-01 | sale                                    | NULL         |    2000.35 |    402 |         2 |
|  9 | 歪歪       | female |  48 | 2015-03-11 | sale                                    | NULL         |    3000.13 |    402 |         2 |
|  8 | 成龍       | male   |  48 | 2010-11-11 | teacher                                 | NULL         |   10000.00 |    401 |         1 |
|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                                 | NULL         |    3500.00 |    401 |         1 |
|  2 | alex       | male   |  78 | 2015-03-02 | teacher                                 | NULL         | 1000000.31 |    401 |         1 |
|  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher                                 | NULL         |    8300.00 |    401 |         1 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
rows in set (0.00 sec)
View Code
mysql> select distinct post, count(id) as emp_count from employee
    -> where salary > 1000
    -> group by post
    -> having count(id) > 1
    -> order by emp_count desc;
+-----------+-----------+
| post      | emp_count |
+-----------+-----------+
| teacher   |         7 |
| sale      |         5 |
| operation |         5 |
+-----------+-----------+
rows in set (0.00 sec)
View Code
#limit限制列印條數 
select * from employee limit 3;
select * from employee order by salary desc limit 1; #找工資最高的那個人的詳細資訊


select * from employee limit 0,5; #從0開始往後再取5個;基本的分頁功能
select * from employee limit 5,5;
select * from employee limit 10,5;
select * from employee limit 15,5;
mysql> select * from employee limit 3;
+----+---------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name    | sex  | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |
+----+---------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
|  1 | egon    | male |  18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使              | NULL         |    7300.33 |    401 |         1 |
|  2 | alex    | male |  78 | 2015-03-02 | teacher                                 | NULL         | 1000000.31 |    401 |         1 |
|  3 | wupeiqi | male |  81 | 2013-03-05 | teacher                                 | NULL         |    8300.00 |    401 |         1 |
+----+---------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
rows in set (0.00 sec)

mysql> select * from employee order by salary desc limit 1;
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
| id | name | sex  | age | hire_date  | post    | post_comment | salary     | office | depart_id |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
|  2 | alex | male |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
row in set (0.00 sec)

mysql> select * from employee limit 0,5;
+----+-----------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name      | sex  | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |
+----+-----------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
|  1 | egon      | male |  18 | 2017-03-01 | 老男孩駐沙河辦事處外交大使              | NULL         |    7300.33 |    401 |         1 |
|  2 | alex      | male |  78 | 2015-03-02 | teacher                                 | NULL         | 1000000.31 |    401 |         1 |
|  3 | wupeiqi   | male |  81 | 2013-03-05 | teacher                                 | NULL         |    8300.00 |    401 |         1 |
|  4 | yuanhao   | male |  73 | 2014-07-01 | teacher                                 | NULL         |    3500.00 |    401 |         1 |
|  5 | liwenzhou | male |  28 | 2012-11-01 | teacher                                 | NULL         |    2100.00 |    401 |         1 |
+----+-----------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
rows in set (0.00 sec)
View Code
#總結:
    語法順序:
        select distinct 欄位1,欄位2,欄位3 from 庫.表 
            where 條件
            group by 分組條件
            having 過濾
            order by 排序欄位
            limit n;

    執行順序:

def from(db,table):
        f=open(r'%s\%s' %(db,table)) #開啟檔案
        return f #返回檔案物件
    
def where(condition,f):
    for line in f:
        if condition:
            yield line #滿足條件再返回,不停地返回

def group(lines): #分組
    pass
    
def having(group_res): #分組,拿到分組的結果
    pass

def distinct(having_res): #拿到having_res的結果
    pass

def order(distinct_res): #拿到distinct_res的結果
    pass
    
def limit(order_res) #
    pass
    
def select(): 
    f=from('db1','t1')  #找表
    lines=where('id>3',f)
    group_res=group(lines)
    having_res=having(group_res)
    distinct_res=distinct(having_res)
    order_res=order(distinct_res)
    res=limit(order_res)
    print(res)
    return res #返回值
    
#正則表示式 查詢 
select * from employee where name like 'jin%';
select * from employee where name regexp '^jin';  #以什麼開頭^
select * from employee where name regexp '^jin.*(g|n)$'; # .*()以什麼結尾 ,以g或者n結尾
mysql> select * from employee where name regexp '^jin';
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
| id | name       | sex    | age | hire_date  | post    | post_comment | salary| office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         |  9000.00 |    401 |         1 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher | NULL         | 30000.00 |    401 |         1 |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
rows in set (0.07 sec)

mysql> select * from employee where name regexp '^jin.*(g|n)$';
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
| id | name       | sex    | age | hire_date  | post    | post_comment | salary| office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
|  6 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         |  9000.00 |    401 |         1 |
|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher | NULL         | 30000.00 |    401 |         1 |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
rows in set (0.00 sec)
View Code

1.3 多表查詢 

 把有關係的表連接合到一起;   department和employee表是多對一的關係。

內連線:只取兩張表的共同部分
select * from employee inner join department on employee.dep_id = department.id ;

左連線:在內連線的基礎上保留左表的記錄
select * from employee left join department on employee.dep_id = department.id ;

右連線:在內連線的基礎上保留右表的記錄
select * from employee right join department on employee.dep_id = department.id ;

全外連線:在內連線的基礎上保留左右兩表沒有對應關係的記錄
select * from employee full join department on employee.dep_id = department.id ; #mysql不支援full join


select * from employee left join department on employee.dep_id = department.id
union
select * from employee right join department on employee.dep_id = department.id ;

 笛卡爾積;在笛卡爾積的基礎之中對錶進行篩選,where,這就是連線;不要用where幹,它是過濾的,有專門的方法