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