今日內容概要
-
如何查詢表
""" select where group by having distinct order by limit regexp like ... """
-
連表操作理論
今日內容詳細
前期表准備
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), #以下是教學部
('tom','male',78,'20150302','teacher',1000000.31,401,1),
('kevin','male',81,'20130305','teacher',8300,401,1),
('tony','male',73,'20140701','teacher',3500,401,1),
('owen','male',28,'20121101','teacher',2100,401,1),
('jack','female',18,'20110211','teacher',9000,401,1),
('jenny','male',18,'19000301','teacher',30000,401,1),
('sank','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);
# 當表字段特別多 展示的時候錯亂 可以使用\G分行展示
select * from emp\G;
# 個別同學的電腦在插入中文的時候還是會出現亂碼或者空白的現象 你可以將字符編碼統一設置成GBK
幾個重要關鍵字的執行順序
# 書寫順序
select id,name from emp where id > 3;
# 執行順序
from
where
select
"""
雖然執行順序和書寫順序不一致 你在寫sql語句的時候可能不知道怎么寫
你就按照書寫順序的方式寫sql
select * 先用*號占位
之后去補全后面的sql語句
最后將*號替換后你想要的具體字段
明天會一直使用 這里先理解
"""
where篩選條件
# 作用:是對整體數據的一個篩選操作
# 1.查詢id大於等於3小於等於6的數據
select id,name,age from emp where id>=3 and id<=6;
select id,name 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的員工的姓名和薪資
"""
模糊查詢
like
% 匹配任意多個字符
_ 匹配任意單個字符
"""
select name,salary from emp where name like '%o%';
# 4.查詢員工姓名是由四個字符組成的 姓名和薪資 char_length() _
select name,salary from emp where name like '____';
select name,salary from emp where char_length(name) = 4;
# 5.查詢id小於3或者id大於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;
group by分組
# 分組實際應用場景 分組應用場景非常的多
男女比例
部門平均薪資
部門禿頭率
國家之間數據統計
# 1 按照部門分組
select * from emp group by post;
"""
分組之后 最小可操作單位應該是組 還不再是組內的單個數據
上述命令在你沒有設置嚴格模式的時候是可正常執行的 返回的是分組之后 每個組的第一條數據 但是這不符合分組的規范:分組之后不應該考慮單個數據 而應該以組為操作單位(分組之后 沒辦法直接獲取組內單個數據)
如果設置了嚴格模式 那么上述命令會直接報錯
"""
set global sql_mode = 'strict_trans_tables,only_full_group_by';
設置嚴格模式之后 分組 默認只能拿到分組的依據
select post from emp group by post;
按照什么分組就只能拿到分組 其他字段不能直接獲取 需要借助於一些方法(聚合函數)
"""
什么時候需要分組啊???
關鍵字
每個 平均 最高 最低
聚合函數
max
min
sum
count
avg
"""
# 1.獲取每個部門的最高薪資
select post,max(salary) from emp group by post;
select post as '部門',max(salary) as '最高薪資' from emp group by post;
select post '部門',max(salary) '最高薪資' from emp group by post;
# as可以給字段起別名 也可以直接省略不寫 但是不推薦 因為省略的話語意不明確 容易錯亂
# 2.獲取每個部門的最低薪資
select post,min(salary) from emp group by post;
# 3.獲取每個部門的平均薪資
select post,avg(salary) from emp group by post;
# 4.獲取每個部門的工資總和
select post,sum(salary) from emp group by post;
# 5.獲取每個部門的人數
select post,count(id) from emp group by post; # 常用 符合邏輯
select post,count(salary) from emp group by post;
select post,count(age) from emp group by post;
select post,count(post_comment) from emp group by post; null不行
# 6.查詢分組之后的部門名稱和每個部門下所有的員工姓名
# group_concat不單單可以支持你獲取分組之后的其他字段值 還支持拼接操作
select post,group_concat(name) from emp group by post;
select post,group_concat(name,'_DSB') from emp group by post;
select post,group_concat(name,':',salary) from emp group by post;
# concat不分組的時候用
select concat('NAME:',name),concat('SAL:',salary) from emp;
# 補充 as語法不單單可以給字段起別名 還可以給表臨時起別名
select emp.id,emp.name from emp;
select emp.id,emp.name from emp as t1; 報錯
select t1.id,t1.name from emp as t1;
# 查詢每個人的年薪 12薪
select name,salary*12 from emp;
分組注意事項
# 關鍵字where和group by同時出現的時候group by必須在where的后面
where先對整體數據進行過濾之后再分組操作
where篩選條件不能使用聚合函數
select id,name,age from emp where max(salary) > 3000;
select max(salary) from emp; # 不分組 默認整體就是一組
# 統計各部門年齡在30歲以上的員工平均薪資
1 先求所有年齡大於30歲的員工
select * from emp where age>30;
2 再對結果進行分組
select * from emp where age>30 group by post;
select post,avg(salary) from emp where age>30 group by post;
having分組之后的篩選條件
"""
having的語法根where是一致的
只不過having是在分組之后進行的過濾操作
即having是可以直接使用聚合函數的
"""
# 統計各部門年齡在30歲以上的員工平均工資並且保留平均薪資大於10000的部門
select post,avg(salary) from emp
where age>30
group by post
having avg(salary) > 10000
;
distinct去重
"""
一定要注意 必須是完全一樣的數據才可以去重!!!
一定不要將逐漸忽視了 有逐漸存在的情況下 是不可能去重的
[
{'id':1,'name':'jason','age':18},
{'id':2,'name':'jason','age':18},
{'id':3,'name':'egon','age':18}
]
ORM 對象關系映射 讓不懂SQL語句的人也能夠非常牛逼的操作數據庫
表 類
一條條的數據 對象
字段對應的值 對象的屬性
你再寫類 就意味着在創建表
用類生成對象 就意味着再創建數據
對象點屬性 就是在獲取數據字段對應的值
目的就是減輕python程序員的壓力 只需要會python面向對象的知識點就可以操作MySQL
"""
select distinct id,age from emp;
select distinct age from emp;
order by排序
select * from emp order by salary;
select * from emp order by salary asc;
select * from emp order by salary desc;
"""
order by默認是升序 asc 該asc可以省略不寫
也可以修改為降序 desc
"""
select * from emp order by age desc,salary asc;
# 先按照age降序排 如果碰到age相同 則再按照salary升序排
# 統計各部門年齡在10歲以上的員工平均工資並且保留平均薪資大於1000的部門,然后對平均工資降序排序
select post,avg(salary) from emp
where age>10
group by post
having avg(salary) > 1000
order by avg(salary) desc
;
limit限制展示條數
select * from emp;
"""針對數據過多的情況 我們通常都是做分頁處理"""
select * from emp limit 3; # 只展示三條數據
select * from emp limit 0,5;
select * from emp limit 5,5;
第一個參數是起始位置
第二個參數是展示條數
正則
select * from emp where name regexp '^j.*(n|y)$';
多表操作
前期表准備
#建表
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',18,201),
('nick','male',28,202),
('owen','male',18,203),
('jerry','female',18,204);
表查詢
select * from dep,emp; # 結果 笛卡爾積
"""
了解即可 不知道也沒關系
"""
select * from emp,dep where emp.dep_id = dep.id;
"""
MySQL也知道 你在后面查詢數據過程中 肯定會經常用到拼表操作
所以特地給你開設了對應的方法
inner join 內連接
left join 左連接
right join 右連接
union 全連接
"""
# inner join 內連接
select * from emp inner join dep on emp.dep_id = dep.id;
# 只拼接兩張表中公有的數據部分
# left join 左連接
select * from emp left join dep on emp.dep_id = dep.id;
# 左表所有的數據都展示出來 沒有對應的項就用NULL
# right join 右連接
select * from emp right join dep on emp.dep_id = dep.id;
# 右表所有的數據都展示出來 沒有對應的項就用NULL
# union 全連接 左右兩表所有的數據都展示出來
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號
2 再去員工表里面篩選出對應的員工
select id from dep where name='技術' or name = '人力資源';
select name from emp where dep_id in (200,201);
select * from emp where dep_id in (select id from dep where name='技術' or name = '人力資源');
總結
表的查詢結果可以作為其他表的查詢條件
也可以通過起別名的方式把它作為一個張虛擬表根其他表關聯
"""
多表查詢就兩種方式
先拼接表再查詢
子查詢 一步一步來
"""
作業
1.整理今日內容
2.完成下列分組查詢練習題(以課上建表代碼為參考)
1. 查詢崗位名以及崗位包含的所有員工名字
2. 查詢崗位名以及各崗位內包含的員工個數
3. 查詢公司內男員工和女員工的個數
4. 查詢崗位名以及各崗位的平均薪資
5. 查詢崗位名以及各崗位的最高薪資
6. 查詢崗位名以及各崗位的最低薪資
7. 查詢男員工與男員工的平均薪資,女員工與女員工的平均薪資
3.練習拼表操作並理解其意義
4.理解子查詢思路體會其意義