先創建表
#創建表
create table employee(
id int not null unique 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
);
#查看錶結構
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 | |
+--------------+-----------------------+------+-----+---------+----------------+
#插入記錄
#三個部門:教學,銷售,運營
insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
('egon','male',18,'20170301','teacher',7300.33,401,1), #以下是教學部
('alex','male',78,'20150302','teacher',1000000.31,401,1),
('wupeiqi','male',81,'20130305','teacher',8300,401,1),
('yuanhao','male',73,'20140701','teacher',3500,401,1),
('liwenzhou','male',28,'20121101','teacher',2100,401,1),
('jingliyang','female',18,'20110211','teacher',9000,401,1),
('jinxin','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)
;
1 1.注意:
2 select * from t1 where 條件 group by 分組字段
3 1.分組只能查詢分組字段,要想查看其餘的利用聚合函數
4 2.聚合函數的分類:count,min,max,avg,group_concat,sum等。
5 3.模糊匹配:用like關鍵字。
6 select * from t1 where name like '%eg%'; #%表示任意字符
7 select * from t1 where name like 'd__l'; #一個下劃線表示一個字符,兩個下劃
線就表示兩個字符
8
4.拷貝表 :create table t2 select * from t1;
9 create table t2 select * from t1 where 1=2 ;
一點小知識複習
一、查詢語法
SELECT 字段1,字段2... FROM 表名
WHERE 條件
GROUP BY field
HAVING 篩選
ORDER BY field
LIMIT 限制條數
二、簡單查詢
#簡單查詢
SELECT id,name,sex,age,hire_date,post,post_comment,salary,office,depart_id
FROM employee;
SELECT * FROM employee;
SELECT name,salary FROM employee;
#避免重複DISTINCT
SELECT DISTINCT post FROM employee;
#通過四則運算查詢
SELECT name, salary*12 FROM employee;
SELECT name, salary*12 AS Annual_salary FROM employee;
SELECT name, salary*12 Annual_salary FROM employee;
#定義顯示格式
CONCAT() 函數用於連接字符串
SELECT CONCAT('姓名: ',name,' 年薪: ', salary*12) AS Annual_salary
FROM employee;
CONCAT_WS() 第一個參數爲分隔符
SELECT CONCAT_WS(':',name,salary*12) AS Annual_salary
FROM employee;
小練習:
1
查出所有員工的名字,薪資,格式爲
<名字:egon> <薪資:3000>
select concat('<名字:',name,'> ' ,'<薪資:',salary,'>' ) from employee;
2 查出所有的崗位(去掉重複)
select distinct depart_id from employee;
3 查出所有員工名字,以及他們的年薪,年薪的字段名爲年薪
select name,salary*12 年薪 from employee;
三、where約束
where字句中可以使用:
1
. 比較運算符:> < >= <= <> !=
2. between 80 and 100 值在10到20之間
3. in(80,90,100) 值是80或90或100
4. like 'eg%'
可以是%或_,
%表示任意多字符
_表示一個字符
like ‘e__n’ :
5. 邏輯運算符:在多個條件直接可以使用邏輯運算符 and or not
#1:單條件查詢
SELECT name FROM employee
WHERE post=‘sale’;
#2:多條件查詢
SELECT name,salary FROM employee
WHERE post=‘teacher’ AND salary>10000;
#3:關鍵字BETWEEN AND
SELECT name,salary FROM employee
WHERE salary BETWEEN 10000 AND 20000;
SELECT name,salary FROM employee
WHERE salary NOT BETWEEN 10000 AND 20000;
#4:關鍵字IS NULL(判斷某個字段是否爲NULL不能用等號,需要用IS)
SELECT name,post_comment FROM employee
WHERE post_comment IS NULL;
SELECT name,post_comment FROM employee
WHERE post_comment IS NOT NULL;
SELECT name,post_comment FROM employee
WHERE post_comment=''; 注意''是空字符串,不是null
ps:
執行
update employee set post_comment='' where id=2;
再用上條查看,就會有結果了
#5:關鍵字IN集合查詢
SELECT name,salary FROM employee
WHERE salary=3000 OR salary=3500 OR salary=4000 OR salary=9000 ;
SELECT name,salary FROM employee
WHERE salary IN (3000,3500,4000,9000) ;
SELECT name,salary FROM employee
WHERE salary NOT IN (3000,3500,4000,9000) ;
#6:關鍵字LIKE模糊查詢
通配符’%’
SELECT * FROM employee
WHERE name LIKE ‘eg%’;
通配符’_’
SELECT * FROM employee
WHERE name LIKE 'al__';
四、having過濾
having和where語法上是一樣的。
select * from employee where id>15;
select * from employee having id>15;
但是having和where不一樣的地方在於以下幾點!!!
#!!!執行優先級從高到低:where > group by > 聚合函數 > having >order by
1.where和having的區別
1. Where 是一個約束聲明,使用Where約束來自數據庫的數據,Where是在結果返回之前起作用的
(先找到表,按照where的約束條件,從表(文件)中取出數據),Where中不能使用聚合函數
2.Having是一個過濾聲明,是在查詢返回結果集以後對查詢結果進行的過濾操作
(先找到表,按照where的約束條件,從表(文件)中取出數據,然後group by分組,
如果沒有group by則所有記錄整體爲一組,然後執行聚合函數,然後使用having對聚合的結果進行過濾),
在Having中可以使用聚合函數。
3.where的優先級比having的優先級高
4.having可以放到group by之後,而where只能放到group by 之前。
驗證不同之處:
1.查看員工的id>15的有多少個
select count(id) from employee where id>15;#正確,分析:where先執行,後執行聚合count(id),
然後select出結果
select count(id) from employee having id>15; #報錯,分析:先執行聚合count(id),後執行having過濾,
#無法對id進行id>15的過濾
#以上兩條sql的順序是
1:找到表employee—>用where過濾---->沒有分組則默認一組執行聚合count(id)—>select執行查看組內id數目
2:找到表employee—>沒有分組則默認一組執行聚合count(id)---->having 基於上一步聚合的結果(此時只有count(id)字段了)
進行id>15的過濾,很明顯,根本無法獲取到id字段
1 ------having-----------
2 select depart_id,count(id) from employee group by depart_id;
3 select depart_id,count(id) from employee group by depart_id having depart_id = 3;
4 select depart_id,count(id) from employee group by depart_id having count(id)>7;
5 select max(salary) 最大工資 from employee where id>2 group by depart_id having count(id)>3;
6 select * from employee where id>7; #查看所有id>7的員工信息
having 舉例
小練習:
- 查詢各崗位內包含的員工個數小於2的崗位名、崗位內包含員工名字、個數
select post,group_concat(name) 員工姓名,count(id) 個數 from employee group by post having count(id)<2; - 查詢各崗位平均薪資大於10000的崗位名、平均工資
select post,avg(salary) from employee group by post having avg(salary)>10000; - 查詢各崗位平均薪資大於10000且小於20000的崗位名、平均工資
select post,avg(salary) from employee group by post having avg(salary) between 10000 and 20000;
五、分組查詢 group by
大前提:可以按照任意字段分組,但分完組後,只能查看分組的那個字段,要想取的組內的其他字段信息,需要藉助函數
單獨使用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;#按照崗位分組,並查看每個組有多少人
強調:
分組:一般相同的多的話就可以分成一組(一定是有重複的字段)
小練習:
- 查詢崗位名以及崗位包含的所有員工名字
select post,group_concat(name) from employee group by post; - 查詢崗位名以及各崗位內包含的員工個數
select post,count(id) from employee group by post; - 查詢公司內男員工和女員工的個數
select sex,count(id) from employee group by sex; - 查詢崗位名以及各崗位的平均薪資
select post,max(salary) from employee group by post; - 查詢崗位名以及各崗位的最高薪資
select post,max(salary) from employee group by post; - 查詢崗位名以及各崗位的最低薪資
select post,min(salary) from employee group by post; - 查詢男員工與男員工的平均薪資,女員工與女員工的平均薪資
select sex,avg(salary) from employee group by sex;
六、關鍵字的執行優先級(重點)
重點中的重點:關鍵字的執行優先級
from
where
group by
having
select
distinct
order by
limit
1.找到表:from
2.拿着where指定的約束條件,去文件/表中取出一條條記錄
3.將取出的一條條記錄進行分組group by,如果沒有group by,則整體作爲一組
4.如果有聚合函數,則將組進行聚合
5.將4的結果過濾:having
6.查出結果:select
7.去重
8.將6的結果按條件排序:order by
9.將7的結果限制顯示條數
七、查詢排序order by
按單列排序
SELECT * FROM employee ORDER BY salary;
SELECT * FROM employee ORDER BY salary ASC;
SELECT * FROM employee ORDER BY salary DESC;
按多列排序:先按照age排序,如果年紀相同,則按照薪資排序
SELECT * from employee
ORDER BY age,
salary DESC;
=order by
1.select * from employee order by salary;#如果不指定,默認就是升序
2.select * from employee order by salary asc;
3.select * from employee order by salary desc;
#先按照年齡升序,當年齡相同的太多,分不清大小時,在按照工資降序
4.select * from employee order by age asc, salary desc;
小例子:
- 查詢所有員工信息,先按照age升序排序,如果age相同則按照hire_date降序排序
select * form employee order by age,hire_date desc; - 查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資升序排列
select post ,avg(salary) from employee group by post having avg(salary)>10000; - 查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資降序排列
select post ,avg(salary) from employee group by post having avg(salary)>10000 desc;
八、使用聚合函數查詢
先from找到表
再用where的條件約束去表中取出記錄
然後進行分組group by,沒有分組則默認一組
然後進行聚合
最後select出結果
示例:
select count() from employee;
select count() from employee where depart_id=1;
select max(salary) from employee;
select min(salary) from employee;
select avg(salary) from employee;
select sum(salary) from employee;
select sum(salary) form employee WHERE depart_id=3;
九、where的補充(使用正則表達式查詢)
1.select * from employee where name regexp '^ale'; #匹配以ale開頭的員工信息
2.select * from employee where name regexp 'on$'; #匹配以on結尾的員工信息
3.select * from employee where name regexp 'n{1,2}'; #匹配name裏面包含1到2個
n的員工信息
小結:對字符串匹配的方式
where name = 'egon';
where name like 'yua%';
where name regexp 'on$';
小練習:
查看所有員工中名字是jin開頭,n或者g結果的員工信息
select * from employee where name regexp ‘^jin.*[ng]$’;
十、限制查詢的記錄數:LIMIT
=limit:限制打印幾條=
1.select * from employee limit 3;#打印前三條
2.像這樣表示的:指的是從哪開始,往後取幾條 (這樣的操作一般用來分頁)
select * from employee limit 0,3;
select * from employee limit 3,4;
select * from employee limit 6,3;
select * from employee limit 9,3;
3.select * from employee order by id desc limit 3;
#查看後三條
小練習
- 分頁顯示,每頁5條
select * from employee limit 0,5;
select * from employee limit 5,5;
select * from employee limit 10,5;