內容迴歸(資料的增刪改)/表單查詢
一.內容回顧
1.SQL語句中的DML語言來實現資料的操作
(1).使用insert實現資料的插入
(2).update實現資料的更新
(3).使用delete實現資料的刪除
(4).使用select查詢資料
2.插入資料(INSERT)
(1).插入完整資料(順序插入)
語法一:insert into 表名(欄位1,欄位2,欄位3...) values (值1,值2,值3...);
語法二:insert into 表名 values (值1,值2,值3...);
(2).指定欄位插入資料
語法:insert into 表名(欄位1,欄位2,欄位3...) values (值1,值2,值3...);
(3).插入多條記錄
語法:insert into 表名 values (值1,值2,值3...), (值1,值2,值3...), (值1,值2,值3...);
(4).插入查詢結果
語法:insert into 表名(欄位1,欄位2,欄位3...) select (欄位1,欄位2,欄位3...) from 表2 where ...;
3.更新資料UPDATE
語法:update 表名 set 欄位1 = 值1, 欄位2 = 值2, where condition;
示例:update mysql.user set password = password("123") where user = "root" and host = "localhost";
4.刪除資料DELETE
語法:delete from 表名 where condition;
示例:delete from mysql.user where password = "";
二.表單查詢
1.語法
1.表單查詢的語法 select 欄位1,欄位2.. from 表名 where 條件 group by field having 篩選 order by field limit 限制條數 2.關鍵字的執行優先順序(重點) (1).重點中的重點 from 找到表where 拿到where指定的約束條件,去檔案/表中取出一條條記錄 group by 將取出的一條條記錄進行分組group by,如果沒有group by,則整體作為一組 having 將分組的結果進行having過濾 select 執行select distinct 去重 order by 將結果按條件排序:order by limit 限制結果的顯示條數
2.建立公司員工表,表的欄位和資料型別
company.employee 員工id id int 姓名 name varchar 性別 sex enum 年齡 age int 入職日期 hire_date date 崗位 post varchar 職位描述 post_comment varchar 薪水 salary double 辦公室 office int 部門編號 depart_id int
3.建立員工表,並插入記錄
#建立表,設定欄位的約束條件 mysql> create table employee( -> id int primary key auto_increment, -> name varchar(20) not null, -> sex enum("male","female") not null default "male", -> age int(3) unsigned not null default 28, -> hire_date date not null, -> post varchar(50), -> post_comment varchar(100), -> salary double(15,2), -> office int, #一個部門一個屋 -> depart_id int -> ); Query OK, 0 rows affected (0.27 sec) #查看錶結構 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 | | +--------------+-----------------------+------+-----+---------+----------------+ 10 rows in set (0.01 sec) #插入記錄 #三個部門:教師,銷售,運營 mysql> insert into employee(name, sex, age, hire_date, post, salary, office, depart_id) values -> ('娃哈哈','male',18,'20170301','幼兒園園長',7300.33,401,1), #以下是教學部 -> ('乳娃娃','male',78,'20150302','teacher',1000000.31,401,1), -> ('爽歪歪','male',81,'20130305','teacher',8300,401,1), -> ('冰紅茶','male',73,'20140701','teacher',3500,401,1), -> ('綠茶','male',28,'20121101','teacher',2100,401,1), -> ('抹茶','female',18,'20110211','teacher',9000,401,1), -> ('花茶','male',18,'19000301','teacher',30000,401,1), -> ('黑茶','male',48,'20101111','teacher',10000,401,1), -> -> ('呵呵','female',48,'20150311','sale',3000.13,402,2),#以下是銷售部門 -> ('嘿嘿','female',38,'20101101','sale',2000.35,402,2), -> ('哈哈','female',18,'20110312','sale',1000.37,402,2), -> ('哼哼','female',18,'20160513','sale',3000.29,402,2), -> ('嗯嗯','female',28,'20170127','sale',4000.33,402,2), -> -> ('青銅','male',28,'20160311','operation',10000.13,403,3), #以下是運營部門 -> ('白銀','male',18,'19970312','operation',20000,403,3), -> ('黃金','female',18,'20130311','operation',19000,403,3), -> ('鉑金','male',18,'20150411','operation',18000,403,3), -> ('鑽石','female',18,'20140512','operation',17000,403,3) -> ; Query OK, 18 rows affected (0.09 sec) Records: 18 Duplicates: 0 Warnings: 0 #查看錶 mysql> select * from employee; +----+-----------+--------+-----+------------+-----------------+--------------+------------+--------+-----------+ | id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id | +----+-----------+--------+-----+------------+-----------------+--------------+------------+--------+-----------+ | 1 | 娃哈哈 | male | 18 | 2017-03-01 | 幼兒園園長 | NULL | 7300.33 | 401 | 1 | | 2 | 乳娃娃 | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 | | 3 | 爽歪歪 | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 | | 4 | 冰紅茶 | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 | | 5 | 綠茶 | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 | | 6 | 抹茶 | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 | | 7 | 花茶 | 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 | +----+-----------+--------+-----+------------+-----------------+--------------+------------+--------+-----------+ 18 rows in set (0.00 sec)View Code
(1).where約束
where子句中可以使用 1.比較運算子:>, <, >=, <=, <>, != 2.between 80 and 100:值在80到100之間 3.in(80,90,100)值是10或20或30 4.like "xiaohuapattern": pattern可以是%或者_; %表示任意多字元, _表示一個字元 5.邏輯運算子:在多個條件直接可以使用邏輯運算子 and or not
(2).驗證
#1.單條件查詢 mysql> select id,emp_name from employee where id > 5; ERROR 1054 (42S22): Unknown column 'emp_name' in 'field list' mysql> select id,name from employee where id > 5; +----+--------+ | id | name | +----+--------+ | 6 | 抹茶 | | 7 | 花茶 | | 8 | 黑茶 | | 9 | 呵呵 | | 10 | 嘿嘿 | | 11 | 哈哈 | | 12 | 哼哼 | | 13 | 嗯嗯 | | 14 | 青銅 | | 15 | 白銀 | | 16 | 黃金 | | 17 | 鉑金 | | 18 | 鑽石 | +----+--------+ 13 rows in set (0.00 sec) #2.多條件查詢 mysql> select name from employee where post = "teacher" and salary > 10000; +-----------+ | name | +-----------+ | 乳娃娃 | | 花茶 | +-----------+ 2 rows in set (0.00 sec) #關鍵字查詢 BETWEEN AND mysql> select name,salary from employee where salary between 1000 and 20000; +-----------+----------+ | name | salary | +-----------+----------+ | 娃哈哈 | 7300.33 | | 爽歪歪 | 8300.00 | | 冰紅茶 | 3500.00 | | 綠茶 | 2100.00 | | 抹茶 | 9000.00 | | 黑茶 | 10000.00 | | 呵呵 | 3000.13 | | 嘿嘿 | 2000.35 | | 哈哈 | 1000.37 | | 哼哼 | 3000.29 | | 嗯嗯 | 4000.33 | | 青銅 | 10000.13 | | 白銀 | 20000.00 | | 黃金 | 19000.00 | | 鉑金 | 18000.00 | | 鑽石 | 17000.00 | +-----------+----------+ 16 rows in set (0.00 sec) mysql> select name,salary from employee where salary between 10000 and 20000; +--------+----------+ | name | salary | +--------+----------+ | 黑茶 | 10000.00 | | 青銅 | 10000.13 | | 白銀 | 20000.00 | | 黃金 | 19000.00 | | 鉑金 | 18000.00 | | 鑽石 | 17000.00 | +--------+----------+ 6 rows in set (0.00 sec) mysql> select name,salary from employee where salary not between 10000 and 20000; +-----------+------------+ | name | salary | +-----------+------------+ | 娃哈哈 | 7300.33 | | 乳娃娃 | 1000000.31 | | 爽歪歪 | 8300.00 | | 冰紅茶 | 3500.00 | | 綠茶 | 2100.00 | | 抹茶 | 9000.00 | | 花茶 | 30000.00 | | 呵呵 | 3000.13 | | 嘿嘿 | 2000.35 | | 哈哈 | 1000.37 | | 哼哼 | 3000.29 | | 嗯嗯 | 4000.33 | +-----------+------------+ 12 rows in set (0.00 sec) #注意""是空字串,不是null mysql> select name,post_comment from employee where post_comment = ""; #無結果 Empty set (0.00 sec) #執行: mysql> update employee set post_comment = "" where id = 2; Query OK, 1 row affected (0.08 sec) Rows matched: 1 Changed: 1 Warnings: 0 #再執行,有結果 mysql> select name,post_comment from employee where post_comment = ""; +-----------+--------------+ | name | post_comment | +-----------+--------------+ | 乳娃娃 | | +-----------+--------------+ 1 row in set (0.00 sec) #關鍵字IN集合查詢 mysql> select name,salary from employee where salary = 3000 or salary = 3500 or salary = 4000 or salary = 9000; +-----------+---------+ | name | salary | +-----------+---------+ | 冰紅茶 | 3500.00 | | 抹茶 | 9000.00 | +-----------+---------+ 2 rows in set (0.00 sec) mysql> select name,salary from employee where salary in(3000,3500,4000,9000); +-----------+---------+ | name | salary | +-----------+---------+ | 冰紅茶 | 3500.00 | | 抹茶 | 9000.00 | +-----------+---------+ 2 rows in set (0.00 sec) mysql> select name,salary from employee where salary not in(3000,3500,4000,9000); +-----------+------------+ | name | salary | +-----------+------------+ | 娃哈哈 | 7300.33 | | 乳娃娃 | 1000000.31 | | 爽歪歪 | 8300.00 | | 綠茶 | 2100.00 | | 花茶 | 30000.00 | | 黑茶 | 10000.00 | | 呵呵 | 3000.13 | | 嘿嘿 | 2000.35 | | 哈哈 | 1000.37 | | 哼哼 | 3000.29 | | 嗯嗯 | 4000.33 | | 青銅 | 10000.13 | | 白銀 | 20000.00 | | 黃金 | 19000.00 | | 鉑金 | 18000.00 | | 鑽石 | 17000.00 | +-----------+------------+ 16 rows in set (0.00 sec) #關鍵字LIKE模糊查詢 mysql> select * from employee where name like "%茶"; +----+-----------+--------+-----+------------+---------+--------------+----------+--------+-----------+ | id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id | +----+-----------+--------+-----+------------+---------+--------------+----------+--------+-----------+ | 4 | 冰紅茶 | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 | | 5 | 綠茶 | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 | | 6 | 抹茶 | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 | | 7 | 花茶 | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 | | 8 | 黑茶 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 | +----+-----------+--------+-----+------------+---------+--------------+----------+--------+-----------+ 5 rows in set (0.00 sec) mysql> select age from employee where name like "_茶"; +-----+ | age | +-----+ | 28 | | 18 | | 18 | | 48 | +-----+ 4 rows in set (0.00 sec)View Code
4.group by 分組查詢
1.首先明確一點:分組發生在where之後,即分組是基於where之後得到的記錄而進行的 2.分組指的是:將所有記錄按照某個相同欄位進行分類,比如針對員工資訊表的職位分組,或者按照性別進行分組等 3.為何要分組: 取每個部門的最高工資 取每個部門的員工數 取男人數和女人數 小竅門:"每"這個字後面的欄位,就是我們分組的依據 4.大前提:可以按照任意欄位分組,但是分組完畢後,比如group by post,只能檢視post欄位,如果想檢視組內資訊,需要藉助於聚合函式
當執行以下sql語句的時候,是以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 | 乳娃娃 | male | 78 | 2015-03-02 | teacher | | 1000000.31 | 401 | 1 | | 1 | 娃哈哈 | male | 18 | 2017-03-01 | 幼兒園園長 | NULL | 7300.33 | 401 | 1 | +----+-----------+--------+-----+------------+-----------------+--------------+------------+--------+-----------+ 4 rows in set (0.00 sec) #由於沒有設定ONLY_FULL_GROUP_BY,於是也可以有結果,預設都是組內的第一條記錄,但其實這是沒有意義的 #想分組就必須要在全域性的SQL的模式設定為ONLY_FULL_GROUP_BY,把這段英文新增到資料庫安裝地址中的my.ini配置檔案中的sql_mode句段後 #檢視MySQL5.7預設的sql_mode如下: mysql> select @@global.sql_mode; +---------------------------------------------------------------+ | @@global.sql_mode | +---------------------------------------------------------------+ | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION | +---------------------------------------------------------------+ 1 row in set (0.00 sec) #設定成功後必須退出客戶端重啟服務端
繼續驗證通過group by分組之後,只能檢視當前欄位,如果想檢視組內資訊,需要藉助於聚合函式
mysql> select * from employee group by post; #報錯 ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db7.employee.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by mysql> select post from employee group by post; +-----------------+ | post | +-----------------+ | operation | | sale | | teacher | | 幼兒園園長 | +-----------------+ 4 rows in set (0.00 sec)
5.聚合函式
max()求最大值 min()求最小值 avg()求平均值 sum()求和 count()求總個數 #強調:聚合函式聚合的是組的內容,若是沒有分組,則預設一組 # 每個部門有多少個員工 select post,count(id) from employee group by post; # 每個部門的最高薪水 select post,max(salary) from employee group by post; # 每個部門的最低薪水 select post,min(salary) from employee group by post; # 每個部門的平均薪水 select post,avg(salary) from employee group by post; # 每個部門的所有薪水 select post,sum(age) from employee group by post;
6.having過濾
HAVING與WHERE不一樣的地方在於
#having後面的欄位只能是分組(group by之後的欄位) #!!!執行優先順序從高到低:where > group by > having #1. Where 發生在分組group by之前,因而Where中可以有任意欄位,但是絕對不能使用聚合函式。 #2. Having發生在分組group by之後,因而Having中可以使用分組的欄位,可以直接取到其他欄位,可以使用聚合函式
mysql> select post,group_concat(name) from emp having salary > 10000; ERROR 1054 (42S22): Unknown column 'post' in 'field list' mysql> select * from employee where salary > 100000; +----+-----------+------+-----+------------+---------+--------------+------------+--------+-----------+ | id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id | +----+-----------+------+-----+------------+---------+--------------+------------+--------+-----------+ | 2 | 乳娃娃 | male | 78 | 2015-03-02 | teacher | | 1000000.31 | 401 | 1 | +----+-----------+------+-----+------------+---------+--------------+------------+--------+-----------+ 1 row in set (0.00 sec) mysql> select * from employee having salary > 1000000; +----+-----------+------+-----+------------+---------+--------------+------------+--------+-----------+ | id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id | +----+-----------+------+-----+------------+---------+--------------+------------+--------+-----------+ | 2 | 乳娃娃 | male | 78 | 2015-03-02 | teacher | | 1000000.31 | 401 | 1 | +----+-----------+------+-----+------------+---------+--------------+------------+--------+-----------+ 1 row in set (0.00 sec) #必須使用group by才能使用group_concat()函式,將所有的name值連線 mysql> select post,group_concat(name) from emp group by post having salary > 10000; ERROR 1054 (42S22): Unknown column 'post' in 'field list'View Code
1.查詢各崗位內包含的員工個數小於2的崗位名、崗位內包含員工名字、個數 select post,group_concat(name),count(1) from employee group by post having count(1) < 2; 2. 查詢各崗位平均薪資大於10000的崗位名、平均工資 select * from employee where salary not in (select avg(salary) as A from employee group by post having A > 10000); select post,avg(salary) as A from employee group by post having A > 10000 3. 查詢各崗位平均薪資大於10000且小於20000的崗位名、平均工資 select post,avg(salary) from employee group by post having avg(salary) between 10000 and 20000;小練習
7.order by 查詢排序
按單列排序 select * from employee order by age; select * from employee order by age ASC; select * from employee order by age DESC; 按多列排序:先按照age升序排序,,如果年紀相同,則id降序 select * from employee order by age ASC, id DESC;
mysql> select * from employee order by age ASC, id DESC; +----+-----------+--------+-----+------------+-----------------+--------------+------------+--------+-----------+ | 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 | 花茶 | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 | | 6 | 抹茶 | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 | | 1 | 娃哈哈 | 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 | 綠茶 | 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 | 冰紅茶 | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 | | 2 | 乳娃娃 | male | 78 | 2015-03-02 | teacher | | 1000000.31 | 401 | 1 | | 3 | 爽歪歪 | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 | +----+-----------+--------+-----+------------+-----------------+--------------+------------+--------+-----------+ 18 rows in set (0.01 sec)驗證多列排序
8.limit限制查詢的記錄數
示例: select * from employee order by salary DESC LIMIT 3; #預設初識位置為0 select * from employee order by salary DESC LIMIT 0,5; #從第0開始,即先查詢出第一條,然後包含這一條在內往後查5條 select * from employee order by salary DESC LIMT 5,5; #從第5開始,即先查詢出第6條,然後包含這一條在內往後查5條
#分頁顯示,每頁5條 #第一頁資料 mysql> select * from employee limit 0,5; +----+-----------+------+-----+------------+-----------------+--------------+------------+--------+-----------+ | id | name | sex | age | hire_date | post | post_comment | salary | office | d