MySQL查詢 一、單表查詢 語法: 1. where約束 2. group by 分組查詢 當執行以下sql語句的時候,是以post欄位查詢了組中的第一條數據,沒有任何意義,因為我們現在想查出當前組的多條記 繼續驗證通過group by分組之後,只能查看當前欄位,如果想查看組內信息,需要藉助於聚合 ...
MySQL查詢
一、單表查詢
語法:
一、單表查詢的語法
SELECT 欄位1,欄位2... FROM 表名
WHERE 條件
GROUP BY field
HAVING 篩選
ORDER BY field
LIMIT 限制條數
二、關鍵字的執行優先順序(重點)
重點中的重點:關鍵字的執行優先順序
from
where
group by
having
select
distinct
order by
limit
1.找到表:from
2.拿著where指定的約束條件,去文件/表中取出一條條記錄
3.將取出的一條條記錄進行分組group by,如果沒有group by,則整體作為一組
4.將分組的結果進行having過濾
5.執行select
6.去重
7.將結果按條件排序:order by
8.限制結果的顯示條數
where約束
1.比較運算符:>、<、>=、<=、<>、!= 2.between 80 and 100 :值在80到100之間 3.in(80,90,100)值是10或20或30 4.like 'xiaomagepattern': pattern可以是%或者_。%小時任意多字元,_表示一個字元 5.邏輯運算符:在多個條件直接可以使用邏輯運算符 and or not
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 | dogfa | male | 78 | 2015-03-02 | teacher | | 1000000.31 | 401 | 1 | | 1 | oldniu | male | 18 | 2017-03-01 | student | NULL | 7300.33 | 401 | 1 | +----+--------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+ 4 rows in set (0.00 sec) #由於沒有設置ONLY_FULL_GROUP_BY,於是也可以有結果,預設都是組內的第一條記錄,但其實這是沒有意義的 如果想分組,則必須要設置全局的sql的模式為ONLY_FULL_GROUP_BY mysql> set global sql_mode='ONLY_FULL_GROUP_BY'; Query OK, 0 rows affected (0.00 sec) #查看MySQL 5.7預設的sql_mode如下: mysql> select @@global.sql_mode; +--------------------+ | @@global.sql_mode | +--------------------+ | ONLY_FULL_GROUP_BY | +--------------------+ 1 row in set (0.00 sec) mysql> exit;#設置成功後,一定要退出,然後重新登錄方可生效 Bye
繼續驗證通過group by分組之後,只能查看當前欄位,如果想查看組內信息,需要藉助於聚合函數
mysql> select * from emp group by post;# 報錯 ERROR 1054 (42S22): Unknown column 'post' in 'group statement' mysql> select post from employee group by post; +-----------------------------------------+ | post | +-----------------------------------------+ | operation | | sale | | teacher | | student | +-----------------------------------------+ 4 rows in set (0.00 sec)
聚合函數
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;
having 過濾
HAVING與WHERE不一樣的地方在於 #!!!執行優先順序從高到低:where > group by > having #1. Where 發生在分組group by之前,因而Where中可以有任意欄位,但是絕對不能使用聚合函數。 #2. Having發生在分組group by之後,因而Having中可以使用分組的欄位,無法直接取到其他欄位,可以使用聚合函數
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;
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 LIMIT 5,5; #從第5開始,即先查詢出第6條,然後包含這一條在內往後查5條
二、多表查詢
準備工作:準備兩張表,部門表(department)、員工表(employee)
create table department(
id int,
name varchar(20)
);
create table employee(
id int primary key auto_increment,
name varchar(20),
sex enum('male','female') not null default 'male',
age int,
dep_id int
);
#插入數據
insert into department values
(200,'技術'),
(201,'人力資源'),
(202,'銷售'),
(203,'運營');
insert into employee(name,sex,age,dep_id) values
('egon','male',18,200),
('alex','female',48,201),
('wupeiqi','male',38,201),
('yuanhao','female',28,202),
('nvshen','male',18,200),
('xiaomage','female',18,204)
;
# 查看表結構和數據
mysql> desc department;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
rows in set (0.19 sec)
mysql> desc employee;
+--------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| sex | enum('male','female') | NO | | male | |
| age | int(11) | YES | | NULL | |
| dep_id | int(11) | YES | | NULL | |
+--------+-----------------------+------+-----+---------+----------------+
rows in set (0.01 sec)
mysql> select * from department;
+------+--------------+
| id | name |
+------+--------------+
| 200 | 技術 |
| 201 | 人力資源 |
| 202 | 銷售 |
| 203 | 運營 |
+------+--------------+
rows in set (0.02 sec)
mysql> select * from employee;
+----+----------+--------+------+--------+
| id | name | sex | age | dep_id |
+----+----------+--------+------+--------+
| 1 | egon | male | 18 | 200 |
| 2 | alex | female | 48 | 201 |
| 3 | wupeiqi | male | 38 | 201 |
| 4 | yuanhao | female | 28 | 202 |
| 5 | nvshen | male | 18 | 200 |
| 6 | xiaomage | female | 18 | 204 |
+----+----------+--------+------+--------+
rows in set (0.00 sec)
ps:觀察兩張表,發現department表中id=203部門在employee中沒有對應的員工,發現employee中id=6的員工在department表中沒有對應關係。
多表連接查詢
兩張表的準備工作已完成,比如現在我要查詢的員工信息以及該員工所在的部門。從該題中,我們看出既要查員工又要查該員工的部門,肯定要將兩張表進行連接查詢,多表連接查詢。
重點:外鏈接語法
語法:
SELECT 欄位列表 FROM 表1 INNER|LEFT|RIGHT JOIN 表2 ON 表1.欄位 = 表2.欄位;
(1)先看第一種情況交叉連接:不適用任何匹配條件。生成笛卡爾積
mysql> select * from employee,department; +----+----------+--------+------+--------+------+--------------+ | id | name | sex | age | dep_id | id | name | +----+----------+--------+------+--------+------+--------------+ | 1 | egon | male | 18 | 200 | 200 | 技術 | | 1 | egon | male | 18 | 200 | 201 | 人力資源 | | 1 | egon | male | 18 | 200 | 202 | 銷售 | | 1 | egon | male | 18 | 200 | 203 | 運營 | | 2 | alex | female | 48 | 201 | 200 | 技術 | | 2 | alex | female | 48 | 201 | 201 | 人力資源 | | 2 | alex | female | 48 | 201 | 202 | 銷售 | | 2 | alex | female | 48 | 201 | 203 | 運營 | | 3 | wupeiqi | male | 38 | 201 | 200 | 技術 | | 3 | wupeiqi | male | 38 | 201 | 201 | 人力資源 | | 3 | wupeiqi | male | 38 | 201 | 202 | 銷售 | | 3 | wupeiqi | male | 38 | 201 | 203 | 運營 | | 4 | yuanhao | female | 28 | 202 | 200 | 技術 | | 4 | yuanhao | female | 28 | 202 | 201 | 人力資源 | | 4 | yuanhao | female | 28 | 202 | 202 | 銷售 | | 4 | yuanhao | female | 28 | 202 | 203 | 運營 | | 5 | nvshen | male | 18 | 200 | 200 | 技術 | | 5 | nvshen | male | 18 | 200 | 201 | 人力資源 | | 5 | nvshen | male | 18 | 200 | 202 | 銷售 | | 5 | nvshen | male | 18 | 200 | 203 | 運營 | | 6 | xiaomage | female | 18 | 204 | 200 | 技術 | | 6 | xiaomage | female | 18 | 204 | 201 | 人力資源 | | 6 | xiaomage | female | 18 | 204 | 202 | 銷售 | | 6 | xiaomage | female | 18 | 204 | 203 | 運營 |
(2)內連接:只連接匹配的行
#找兩張表共有的部分,相當於利用條件從笛卡爾積結果中篩選出了匹配的結果 #department沒有204這個部門,因而employee表中關於204這條員工信息沒有匹配出來 mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee inner join department on employee.dep_id=department.id; +----+---------+------+--------+--------------+ | id | name | age | sex | name | +----+---------+------+--------+--------------+ | 1 | egon | 18 | male | 技術 | | 2 | alex | 48 | female | 人力資源 | | 3 | wupeiqi | 38 | male | 人力資源 | | 4 | yuanhao | 28 | female | 銷售 | | 5 | nvshen | 18 | male | 技術 | +----+---------+------+--------+--------------+ rows in set (0.00 sec) #上述sql等同於 mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee,department where employee.dep_id=department.id;
(3)外鏈接之左連接:優先顯示左表全部記錄
#以左表為準,即找出所有員工信息,當然包括沒有部門的員工 #本質就是:在內連接的基礎上增加左邊有,右邊沒有的結果 mysql> select employee.id,employee.name,department.name as depart_name from employee left join department on employee.dep_id=department.id; +----+----------+--------------+ | id | name | depart_name | +----+----------+--------------+ | 1 | egon | 技術 | | 5 | nvshen | 技術 | | 2 | alex | 人力資源 | | 3 | wupeiqi | 人力資源 | | 4 | yuanhao | 銷售 | | 6 | xiaomage | NULL | +----+----------+--------------+ 6 rows in set (0.00 sec)
(4) 外鏈接之右連接:優先顯示右表全部記錄
#以右表為準,即找出所有部門信息,包括沒有員工的部門 #本質就是:在內連接的基礎上增加右邊有,左邊沒有的結果 mysql> select employee.id,employee.name,department.name as depart_name from employee right join department on employee.dep_id=department.id; +------+---------+--------------+ | id | name | depart_name | +------+---------+--------------+ | 1 | egon | 技術 | | 2 | alex | 人力資源 | | 3 | wupeiqi | 人力資源 | | 4 | yuanhao | 銷售 | | 5 | nvshen | 技術 | | NULL | NULL | 運營 | +------+---------+--------------+ rows in set (0.00 sec)
(5) 全外連接:顯示左右兩個表全部記錄
#外連接:在內連接的基礎上增加左邊有右邊沒有的和右邊有左邊沒有的結果 #註意:mysql不支持全外連接 full JOIN #強調:mysql可以使用此種方式間接實現全外連接 語法:select * from employee left join department on employee.dep_id = department.id union all select * from employee right join department on employee.dep_id = department.id; mysql> 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 ; +------+----------+--------+------+--------+------+--------------+ | id | name | sex | age | dep_id | id | name | +------+----------+--------+------+--------+------+--------------+ | 1 | egon | male | 18 | 200 | 200 | 技術 | | 5 | nvshen | male | 18 | 200 | 200 | 技術 | | 2 | alex | female | 48 | 201 | 201 | 人力資源 | | 3 | wupeiqi | male | 38 | 201 | 201 | 人力資源 | | 4 | yuanhao | female | 28 | 202 | 202 | 銷售 | | 6 | xiaomage | female | 18 | 204 | NULL | NULL | | NULL | NULL | NULL | NULL | NULL | 203 | 運營 | +------+----------+--------+------+--------+------+--------------+ rows in set (0.01 sec) #註意 union與union all的區別:union會去掉相同的紀錄
符合條件連接查詢
示例1:以內連接的方式查詢employee和department表,並且employee表中的age欄位值必須大於25,即找出年齡大於25歲的員工以及員工所在的部門
select employee.name,department.name from employee inner join department on employee.dep_id = department.id where age > 25;
示例2:以內連接的方式查詢employee和department表,並且以age欄位的升序方式顯示。
select employee.id,employee.name,employee.age,department.name from employee,department where employee.dep_id = department.id and age > 25 order by age asc;
子查詢
#1:子查詢是將一個查詢語句嵌套在另一個查詢語句中。 #2:內層查詢語句的查詢結果,可以為外層查詢語句提供查詢條件。 #3:子查詢中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等關鍵字 #4:還可以包含比較運算符:= 、 !=、> 、<等
例子:
(1)帶in關鍵字的子查詢
#查詢平均年齡在25歲以上的部門名 select id,name from department where id in (select dep_id from employee group by dep_id having avg(age) > 25); # 查看技術部員工姓名 select name from employee where dep_id in (select id from department where name='技術'); #查看不足1人的部門名 select name from department where id not in (select dep_id from employee group by dep_id);
(2)帶比較運算符的子查詢
#比較運算符:=、!=、>、>=、<、<=、<> #查詢大於所有人平均年齡的員工名與年齡 mysql> select name,age from employee where age > (select avg(age) from employee); +---------+------+ | name | age | +---------+------+ | alex | 48 | | wupeiqi | 38 | +---------+------+ #查詢大於部門內平均年齡的員工名、年齡 思路: (1)先對員工表(employee)中的人員分組(group by),查詢出dep_id以及平均年齡。 (2)將查出的結果作為臨時表,再對根據臨時表的dep_id和employee的dep_id作為篩選條件將employee表和臨時表進行內連接。 (3)最後再將employee員工的年齡是大於平均年齡的員工名字和年齡篩選。 mysql> select t1.name,t1.age from employee as t1 inner join (select dep_id,avg(age) as avg_age from employee group by dep_id) as t2 on t1.dep_id = t2.dep_id where t1.age > t2.avg_age; +------+------+ | name | age | +------+------+ | alex | 48 |
(3) 帶EXISTS關鍵字的子查詢
#EXISTS關字鍵字表示存在。在使用EXISTS關鍵字時,內層查詢語句不返回查詢的記錄。而是返回一個真假值。True或False #當返回True時,外層查詢語句將進行查詢;當返回值為False時,外層查詢語句不進行查詢 #department表中存在dept_id=203,Ture mysql> select * from employee where exists (select id from department where id=200); +----+----------+--------+------+--------+ | id | name | sex | age | dep_id | +----+----------+--------+------+--------+ | 1 | egon | male | 18 | 200 | | 2 | alex | female | 48 | 201 | | 3 | wupeiqi | male | 38 | 201 | | 4 | yuanhao | female | 28 | 202 | | 5 | nvshen | male | 18 | 200 | | 6 | xiaomage | female | 18 | 204 | +----+----------+--------+------+--------+ #department表中存在dept_id=205,False mysql> select * from employee where exists (select id from department where id=204); Empty set (0.00 sec)