mysql分组查询学生平均年龄_8.21MySQL(四)基本查询语句及方法、连表、子查询...
一、基本查詢語句及方法
sql語句書寫順序
select id,name from emp where id > 3 and id < 6;
sql語句執(zhí)行順序
from? # 確定到底是哪張表
where? # 根據(jù)過濾條件,篩選數(shù)據(jù)
select? # 拿出篩選出來的數(shù)據(jù)中的某些字段
select * from emp\G;
當(dāng)表字段特別多的時候,結(jié)果的排版可能會出現(xiàn)混亂的現(xiàn)象,你可以在查詢語句加\G來規(guī)范查詢結(jié)果
二、from、where
1.查詢id大于等于3小于等于6的數(shù)據(jù)
select * from emp where id >= 3 and id <= 6;
select * from emp where id between 3 and 6;
上述語句完全等價
2.查詢薪資是20000或者18000或者17000的數(shù)據(jù)
select id,name from emp where salary = 20000 or salary = 18000 or salary = 17000;
select id,name from emp where salary in (20000,18000,17000);
上述語句完全等價
3.查詢員工姓名中包含o字母的員工姓名和薪資
模糊匹配:like
%:匹配多個任意字符
_:匹配一個任意字符
select name,salary from emp where name like '%o%';
4.查詢員工姓名是由四個字符組成的員工姓名與其薪資
_:匹配一個任意字符
select name,salary from emp where name like '____';
5.查詢id小于3或者大于6的數(shù)據(jù)
select * from emp where id < 3 or id > 6;
select * from emp where id not between 3 and 6;
6.查詢薪資不在20000,18000,17000范圍的數(shù)據(jù)
select id,name 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;? #?MySQL對大小寫不敏感
三、group by:分組
1.按部門分組
select * from emp group by post;? # 嚴(yán)格模式下會報錯
分組之后應(yīng)該做到最小單位是組,而不應(yīng)該再展示組內(nèi)的單個數(shù)據(jù)信息
MySQL中分組之后,只能拿到分組的字段信息,無法直接獲取其他字段信息
但是你可以通過其他方法(聚合函數(shù))簡介的獲取
如果你的MySQL不報錯,說明嚴(yán)格模式?jīng)]有設(shè)置
如何設(shè)置
show variables like '%mode%';
set session? 當(dāng)前窗口有效
set global? 全局有效
set global sql_mode="strict_trans_tables,only_full_group_by";
select * from emp group by post;? # 設(shè)置嚴(yán)格模式后輸入,會報錯
select id,name from emp group by post;? # 報錯
正確寫法:
select name from emp group by post;
2.獲取每個部門的最高工資
需要用到聚合函數(shù):max、min、avg、sum、count
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;
兩種都可以,推薦使用第一種,結(jié)構(gòu)清晰
每個部門的最低工資
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;
每個部門的人數(shù)
select post,count(age) from emp group by post;? # 可以
select post,count(salary) from emp group by post;? # 可以
select post,count(id) from emp group by post;? # 可以,且推薦使用
select post,count(post_comment) from emp group by post;? # 空字段不行
在統(tǒng)計分組內(nèi)個數(shù)的時候,括號內(nèi)填寫任意非空字段都可以完成計數(shù)
推薦使用能夠唯一標(biāo)識數(shù)據(jù)的字段,比如id字段
聚合函數(shù)會自動將每一個分組內(nèi)的單個數(shù)據(jù)做想要的計算,無需你考慮
3.查詢分組之后的部門名稱和每個部門下所有的學(xué)生姓名
group_concat():(分組之后用)能夠拿到分組后每一個數(shù)據(jù)指定字段(可以是多個)對應(yīng)的值
select post,group_concat(name) from emp group by post;
select post,group_concat('DSB',name) from emp group by post;? # 不僅可以用來顯示除分組外字段,還有拼接字符串的作用
select post,group_concat(name,": ",salary) from emp group by post;? #?拼接字符串
concat:(不分組時用)拼接字符串達(dá)到更好的顯示效果
select concat("NAME: ",name),concat("SAL: ",salary) from emp;
小技巧:
concat就是用來幫你拼接數(shù)據(jù)的
concat:不分組情況下使用
group_concat:分組之后使用
4.查詢每個員工的年薪
select name,salary*12 from emp;
5.補(bǔ)充
剛開始查詢表,一定要按照最基本的步驟,先確定是哪張表,再確定查這張表有沒有限制條件,再確定是否需要分類,最后再確定需要什么字段對應(yīng)的信息
你應(yīng)該將每一步操作產(chǎn)生的結(jié)果都當(dāng)成是一張新的表,然后基于該表再進(jìn)行其他的操作
聚合函數(shù) max min sum count avg只能在分組之后使用
如果一張表沒有寫group by,默認(rèn)所有的數(shù)據(jù)就是一組
6.統(tǒng)計各部門年齡在30歲以上的員工平均工資
先獲取年輕在30歲以上的員工
select * from emp where?age > 30;
再分組求平均工資
select post,avg(salary) from emp where age > 30 group by post;
寫sql語句的時候,一定不要一口氣寫完
前期先按照步驟一步步寫
寫一步查詢看一下結(jié)果然后基于當(dāng)前結(jié)果再往后寫
四、having
跟where是一模一樣的,也是用來篩選數(shù)據(jù)的
但是having是跟在group by之后的
where是對整體數(shù)據(jù)做一個初步的篩選
而having是對分組之后的數(shù)據(jù)再進(jìn)行一次針對性的篩選
1、統(tǒng)計各部門年齡在30歲以上的員工平均工資,并且保留平均工資大于10000的部門
select post,avg(salary) from emp where age > 30 group by post having avg(salary) > 10000;
select post,avg(salary) from emp where age > 30 group by post where avg(salary) > 10000;? # 報錯
強(qiáng)調(diào):
having必須在group by后面使用
select * from emp having avg(salary) > 10000;? # 報錯
執(zhí)行順序
from
where
group by
having
select
五、distinct去重
對重復(fù)的數(shù)據(jù)進(jìn)行一個去重
去重必須數(shù)據(jù)是一模一樣的才能去重
只要有一個不一樣,都不能算是的重復(fù)的數(shù)據(jù)
select distinct age from emp;
執(zhí)行順序
from
where
group by
having
select
distinct
六、order by:排序
默認(rèn)是升序 asc
select * from emp order by salary;??# 默認(rèn)升序排
select * from emp order by salary asc;
上下等價
也可以變成降序 desc
select * from emp order by salary desc;??# 降序排
先按照age做升序,age相同的情況下再按照salary做升序
select * from emp order by age,salary;
先按照age做升序,age相同的情況下再按照salary做降序
select * from emp order by age asc,salary desc;
統(tǒng)計各部門年齡在10歲以上的員工平均工資,并且保留平均工資大于1000的部門,然后對平均工資進(jìn)行排序
select post,avg(salary) from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary);
七、limit:限制展示數(shù)據(jù)的條數(shù)
select * from emp limit 5;? # 只展示數(shù)據(jù)的五條
select * from emp limit 5,5;
當(dāng)limit只有一個參數(shù)的時候,表示的是只展示幾條
當(dāng)limit有兩個參數(shù)的時候,第一個參數(shù)表示的是起始位置,第二個參數(shù)表示從起始位置開始往后展示的條數(shù)
查詢工資最高的人的詳細(xì)信息
(1)先按照薪資排序
(2)再用limit限制,只取一條
select * from emp order by salary desc limit 1;
八、正則
在編程中,只要看到reg開頭的,基本上都是跟正則相關(guān)
select * from emp where name regexp '^j.*(n|y)$';
九、多表查詢
1.連表查詢
select * from emp,dep;? # 左表一條記錄與右表所有記錄都對應(yīng)一遍>>>笛卡爾積
將所有的數(shù)據(jù)都對應(yīng)了一遍,雖然不合理但是其中有合理的數(shù)據(jù),現(xiàn)在我們需要做的就是找出合理的數(shù)據(jù)
查詢員工及所在部門的信息
select * from emp,dep where emp.dep_id = dep.id;
查詢部門為技術(shù)部的員工及部門信息
select * from emp,dep where emp.dep_id = dep.id and dep.name = '技術(shù)';
1、內(nèi)連接:只取兩張表有對應(yīng)關(guān)系的記錄(inner join)
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 = "技術(shù)";
2、左連接: 在內(nèi)連接的基礎(chǔ)上保留左表沒有對應(yīng)關(guān)系的記錄(left join)
select * from emp left join dep on emp.dep_id = dep.id;
3、右連接: 在內(nèi)連接的基礎(chǔ)上保留右表沒有對應(yīng)關(guān)系的記錄(right join)
select * from emp right join dep on emp.dep_id = dep.id;
4、全連接:在內(nèi)連接的基礎(chǔ)上保留左、右面表沒有對應(yīng)關(guān)系的的記錄(union)
只要將左連接和右連接的sql語句,加一個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;
2.子查詢
就是將一個查詢語句的結(jié)果用括號括起來當(dāng)作另外一個查詢語句的條件去用
1.查詢部門是技術(shù)或者人力資源的員工信息
先獲取技術(shù)部和人力資源部的id號,再去員工表里面根據(jù)前面的id篩選出符合要求的員工信息
select * from emp where dep_id in (select id from dep where name = "技術(shù)" or name = "人力資源");
2.每個部門最新入職的員工
思路:先查每個部門最新入職的員工,再按部門對應(yīng)上聯(lián)表查詢
select t1.id,t1.name,t1.hire_date,t1.post,t2.* fromemp as t1
inner join
(select post,max(hire_date) as max_datefromemp group by post) as t2
on t1.post=t2.post
where t1.hire_date=t2.max_date
;
可以給表起別名
可以給查詢出來的虛擬表起別名
可以給字段起別名
記住一個規(guī)律,表的查詢結(jié)果可以作為其他表的查詢條件,也可以通過起別名的方式把它作為一張?zhí)摂M表去跟其他表做關(guān)聯(lián)查詢
總結(jié)
以上是生活随笔為你收集整理的mysql分组查询学生平均年龄_8.21MySQL(四)基本查询语句及方法、连表、子查询...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022年全球及中国血清降钙素原市场专项
- 下一篇: 【蓝桥杯C/C++】专题五:DFS深度优