先建立表html
#建立表 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) ;
![](http://static.javashuo.com/static/loading.gif)
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 ;
1、查詢語法
SELECT 字段1,字段2... FROM 表名
WHERE 條件
GROUP BY field
HAVING 篩選
ORDER BY field
LIMIT 限制條數
2、簡單查詢
#簡單查詢 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;
小練習:mysql
1 查出全部員工的名字,薪資,格式爲 <名字:egon> <薪資:3000> select concat('<名字:',name,'> ' ,'<薪資:',salary,'>' ) from employee; 2 查出全部的崗位(去掉重複) select distinct depart_id from employee; 3 查出全部員工名字,以及他們的年薪,年薪的字段名爲年薪 select name,salary*12 年薪 from employee;
3、where約束
where字句中可使用:正則表達式
1. 比較運算符:> < >= <= <> !=
2. between 80 and 100 值在10到20之間
3. in(80,90,100) 值是80或90或100
4. like 'eg%'
能夠是%或_,
%表示任意多字符
_表示一個字符 sql
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__';
4、having過濾
having和where語法上是同樣的。ide
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 以前。
驗證不一樣之處:post
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字段
![](http://static.javashuo.com/static/loading.gif)
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的員工信息
小練習:url
1. 查詢各崗位內包含的員工個數小於2的崗位名、崗位內包含員工名字、個數 select post,group_concat(name) 員工姓名,count(id) 個數 from employee group by post having count(id)<2; 2. 查詢各崗位平均薪資大於10000的崗位名、平均工資 select post,avg(salary) from employee group by post having avg(salary)>10000; 3. 查詢各崗位平均薪資大於10000且小於20000的崗位名、平均工資 select post,avg(salary) from employee group by post having avg(salary) between 10000 and 20000;
5、分組查詢 group by
大前提:能夠按照任意字段分組,但分完組後,只能查看分組的那個字段,要想取的組內的其餘字段信息,須要藉助函數spa
單獨使用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;#按照崗位分組,並查看每一個組有多少人
強調:
分組:通常相同的多的話就能夠分紅一組(必定是有重複的字段)
小練習:
1. 查詢崗位名以及崗位包含的全部員工名字 select post,group_concat(name) from employee group by post;
2. 查詢崗位名以及各崗位內包含的員工個數 select post,count(id) from employee group by post;
3. 查詢公司內男員工和女員工的個數 select sex,count(id) from employee group by sex;
4. 查詢崗位名以及各崗位的平均薪資 select post,max(salary) from employee group by post;
5. 查詢崗位名以及各崗位的最高薪資 select post,max(salary) from employee group by post;
6. 查詢崗位名以及各崗位的最低薪資 select post,min(salary) from employee group by post;
7. 查詢男員工與男員工的平均薪資,女員工與女員工的平均薪資 select sex,avg(salary) from employee group by sex;
6、關鍵字的執行優先級(重點)
重點中的重點:關鍵字的執行優先級 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的結果限制顯示條數
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;
小例子:
1. 查詢全部員工信息,先按照age升序排序,若是age相同則按照hire_date降序排序 select * form employee order by age,hire_date desc; 2. 查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資升序排列 select post ,avg(salary) from employee group by post having avg(salary)>10000; 3. 查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資降序排列 select post ,avg(salary) from employee group by post having avg(salary)>10000 desc;
8、使用聚合函數查詢
先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;
9、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]$';
10、限制查詢的記錄數: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; #查看後三條