單表查詢
前期表準備python
create table emp( 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 ); #插入記錄 #三個部門:教學,銷售,運營 insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values ('jason','male',18,'20170301','張江第一帥形象代言',7300.33,401,1), #如下是教學部 ('egon','male',78,'20150302','teacher',1000000.31,401,1), ('kevin','male',81,'20130305','teacher',8300,401,1), ('tank','male',73,'20140701','teacher',3500,401,1), ('owen','male',28,'20121101','teacher',2100,401,1), ('jerry','female',18,'20110211','teacher',9000,401,1), ('nick','male',18,'19000301','teacher',30000,401,1), ('sean','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) ; #ps:若是在windows系統中,插入中文字符,select的結果爲空白,能夠將全部字符編碼統一設置成gbk1.語法執行順序
# 初識查詢語句 select id,name from emp where id >= 3 and id <= 6; # 前後順序 from where select2.where約束條件
# 1.查詢id大於等於3小於等於6的數據 select id,name from emp where id >= 3 and id <= 6; select * from emp where id between 3 and 6; # 2.查詢薪資是20000或者18000或者17000的數據 select * from emp where salary = 20000 or salary = 18000 or salary = 17000; select * from emp where salary in (20000,18000,17000); # 簡寫 # 3.查詢員工姓名中包含o字母的員工姓名和薪資 # 在你剛開始接觸mysql查詢的時候,建議你按照查詢的優先級順序拼寫出你的sql語句 """ 先是查哪張表 from emp 再是根據什麼條件去查 where name like ‘%o%’ 再是對查詢出來的數據篩選展現部分 select name,salary """ select name,salary from emp where name like '%o%'; # 4.查詢員工姓名是由四個字符組成的員工姓名與其薪資 select name,salary from emp where name like '____'; select name,salary from emp where char_length(name) = 4; # 5.查詢id小於3或者大於6的數據 select * from emp where id not between 3 and 6; # 6.查詢薪資不在20000,18000,17000範圍的數據 select * from emp where salary not in (20000,18000,17000); # 7.查詢崗位描述爲空的員工名與崗位名 針對null不能用等號,只能用is select name,post from emp where post_comment = NULL; # 查詢爲空! select name,post from emp where post_comment is NULL; select name,post from emp where post_comment is not NULL;3.group by
# 數據分組應用場景:每一個部門的平均薪資,男女比例等 # 1.按部門分組 select * from emp group by post; # 分組後取出的是每一個組的第一條數據 select id,name,sex from emp group by post; # 驗證 """ 設置sql_mode爲only_full_group_by,意味着之後但凡分組,只能取到分組的依據, 不該該在去取組裏面的單個元素的值,那樣的話分組就沒有意義了,由於不分組就是對單個元素信息的隨意獲取 """ set global sql_mode="strict_trans_tables,only_full_group_by"; # 從新連接客戶端 select * from emp group by post; # 報錯 select id,name,sex from emp group by post; # 報錯 select post from emp group by post; # 獲取部門信息 # 強調:只要分組了,就不可以再「直接」查找到單個數據信息了,只能獲取到組名 # 2.獲取每一個部門的最高工資 # 以組爲單位統計組內數據>>>聚合查詢(彙集到一塊兒合成爲一個結果) # 每一個部門的最高工資 select post,max(salary) from emp group by post; # 每一個部門的最低工資 select post,min(salary) from emp group by post; # 每一個部門的平均工資 select post,avg(salary) from emp group by post; # 每一個部門的工資總和 select post,sum(salary) from emp group by post; # 每一個部門的人數 select post,count(id) from emp group by post; # 3.查詢分組以後的部門名稱和每一個部門下全部的學生姓名 # group_concat(分組以後用)不只能夠用來顯示除分組外字段還有拼接字符串的做用 select post,group_concat(name) from emp group by post; select post,group_concat(name,"_SB") from emp group by post; select post,group_concat(name,": ",salary) from emp group by post; select post,group_concat(salary) from emp group by post; # 4.補充concat(不分組時用)拼接字符串達到更好的顯示效果 as語法使用 select name as 姓名,salary as 薪資 from emp; select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪資 from emp; # 補充as語法 便可以給字段起別名也能夠給表起 select emp.id,emp.name from emp as t1; # 報錯 由於表名已經被你改爲了t1 select t1.id,t1.name from emp as t1; # 查詢四則運算 # 查詢每一個人的年薪 select name,salary*12 as annual_salary from emp; select name,salary*12 annual_salary from emp; # as能夠省略練習題
# 剛開始查詢表,必定要按照最基本的步驟,先肯定是哪張表,再肯定查這張表也沒有限制條件,再肯定是否須要分類,最後再肯定須要什麼字段對應的信息 1. 查詢崗位名以及崗位包含的全部員工名字 2. 查詢崗位名以及各崗位內包含的員工個數 3. 查詢公司內男員工和女員工的個數 4. 查詢崗位名以及各崗位的平均薪資 5. 查詢崗位名以及各崗位的最高薪資 6. 查詢崗位名以及各崗位的最低薪資 7. 查詢男員工與男員工的平均薪資,女員工與女員工的平均薪資 """ 參考答案: select post,group_concat(name) from emp group by post; select post,count(id) from emp group by post; select sex,count(id) from employee group by sex; select post,avg(salary) from emp 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; """ # 關鍵字where group by同時出現的狀況下,group by必須在where以後 # where先對整張表進行一次篩選,如何group by再對篩選事後的表進行分組 # 如何驗證where是在group by以前執行而不是以後 利用聚合函數 由於聚合函數只能在分組以後才能使用 select id,name,age from emp where max(salary) > 3000; # 報錯! select max(salary) from emp; # 正常運行,不分組意味着每個人都是一組,等運行到max(salary)的時候已經通過where,group by操做了,只不過咱們都沒有寫這些條件 # 語法順序 select from where group by # 再識執行順序 from where group by select 八、統計各部門年齡在30歲以上的員工平均工資 select post,avg(salary) from emp where age > 30 group by post; # 對where過濾出來的虛擬表進行一個分組 # 還不明白能夠分步執行查看結構 select * from emp where age>30; # 基於上面的虛擬表進行分組 select * from emp where age>=30 group by post;4.having
截止目前已經學習的語法mysql
select 查詢字段1,查詢字段2,... from 表名 where 過濾條件 group by分組依據 # 語法這麼寫,可是執行順序卻不同 from where group by selecthaving的語法格式與where一致,只不過having是在分組以後進行的過濾,即where雖然不能用聚合函數,可是having能夠!sql
一、統計各部門年齡在30歲以上的員工平均工資,而且保留平均工資大於10000的部門 select post,avg(salary) from emp where age >= 30 group by post having avg(salary) > 10000; # 若是不信你能夠將having取掉,查看結果,對比便可驗證having用法! #強調:having必須在group by後面使用 select * from emp having avg(salary) > 10000; # 報錯5.distinct
# 對有重複的展現數據進行去重操做 select distinct post from emp;6.order by
select * from emp order by salary asc; #默認升序排 select * from emp order by salary desc; #降序排 select * from emp order by age desc; #降序排 #先按照age降序排,在年輕相同的狀況下再按照薪資升序排 select * from emp order by age desc,salary asc; # 統計各部門年齡在10歲以上的員工平均工資,而且保留平均工資大於1000的部門,而後對平均工資進行排序 select post,avg(salary) from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary) ;7.limit
# 限制展現條數 select * from emp limit 3; # 查詢工資最高的人的詳細信息 select * from emp order by salary desc limit 1; # 分頁顯示 select * from emp limit 0,5; # 第一個參數表示起始位置,第二個參數表示的是條數,不是索引位置 select * from emp limit 5,5;8.正則
select * from emp where name regexp '^j.*(n|y)$';多表查詢
表建立windows
#建表 create table dep( id int, name varchar(20) ); create table emp( id int primary key auto_increment, name varchar(20), sex enum('male','female') not null default 'male', age int, dep_id int ); #插入數據 insert into dep values (200,'技術'), (201,'人力資源'), (202,'銷售'), (203,'運營'); insert into emp(name,sex,age,dep_id) values ('jason','male',18,200), ('egon','female',48,201), ('kevin','male',38,201), ('nick','female',28,202), ('owen','male',18,200), ('jerry','female',18,204) ; # 當初爲何咱們要分表,就是爲了方便管理,在硬盤上確實是多張表,可是到了內存中咱們應該把他們再拼成一張表進行查詢才合理表查詢函數
select * from emp,dep; # 左表一條記錄與右表全部記錄都對應一遍>>>笛卡爾積 # 將全部的數據都對應了一遍,雖然不合理可是其中有合理的數據,如今咱們須要作的就是找出合理的數據 # 查詢員工及所在部門的信息 select * from emp,dep where emp.dep_id = dep.id; # 查詢部門爲技術部的員工及部門信息 select * from emp,dep where emp.dep_id = dep.id and dep.name = '技術'; # 將兩張表關聯到一塊兒的操做,有專門對應的方法 # 一、內鏈接:只取兩張表有對應關係的記錄 select * from emp inner join dep on emp.dep_id = dep.id; select * from emp inner join dep on emp.dep_id = dep.id where dep.name = "技術"; # 二、左鏈接: 在內鏈接的基礎上保留左表沒有對應關係的記錄 select * from emp left join dep on emp.dep_id = dep.id; # 三、右鏈接: 在內鏈接的基礎上保留右表沒有對應關係的記錄 select * from emp right join dep on emp.dep_id = dep.id; # 四、全鏈接:在內鏈接的基礎上保留左、右面表沒有對應關係的的記錄 select * from emp left join dep on emp.dep_id = dep.id union select * from emp right join dep on emp.dep_id = dep.id;子查詢
# 就是將一個查詢語句的結果用括號括起來看成另一個查詢語句的條件去用 # 1.查詢部門是技術或者人力資源的員工信息 """ 先獲取技術部和人力資源部的id號,再去員工表裏面根據前面的id篩選出符合要求的員工信息 """ select * from emp where dep_id in (select id from dep where name = "技術" or name = "人力資源"); # 2.每一個部門最新入職的員工 思路:先查每一個部門最新入職的員工,再按部門對應上聯表查詢 select t1.id,t1.name,t1.hire_date,t1.post,t2.* from emp as t1 inner join (select post,max(hire_date) as max_date from emp group by post) as t2 on t1.post = t2.post where t1.hire_date = t2.max_date ; """ 記住一個規律,表的查詢結果能夠做爲其餘表的查詢條件,也能夠經過其別名的方式把它做爲一張虛擬表去跟其餘表作關聯查詢 """ select * from emp inner join dep on emp.dep_id = dep.id;