日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql分组查询学生平均年龄_8.21MySQL(四)基本查询语句及方法、连表、子查询...

發布時間:2024/3/13 数据库 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql分组查询学生平均年龄_8.21MySQL(四)基本查询语句及方法、连表、子查询... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、基本查詢語句及方法

sql語句書寫順序

select id,name from emp where id > 3 and id < 6;

sql語句執行順序

from? # 確定到底是哪張表

where? # 根據過濾條件,篩選數據

select? # 拿出篩選出來的數據中的某些字段

select * from emp\G;

當表字段特別多的時候,結果的排版可能會出現混亂的現象,你可以在查詢語句加\G來規范查詢結果

二、from、where

1.查詢id大于等于3小于等于6的數據

select * from emp where id >= 3 and id <= 6;

select * from emp where id between 3 and 6;

上述語句完全等價

2.查詢薪資是20000或者18000或者17000的數據

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的數據

select * from emp where id < 3 or id > 6;

select * from emp where id not between 3 and 6;

6.查詢薪資不在20000,18000,17000范圍的數據

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;? # 嚴格模式下會報錯

分組之后應該做到最小單位是組,而不應該再展示組內的單個數據信息

MySQL中分組之后,只能拿到分組的字段信息,無法直接獲取其他字段信息

但是你可以通過其他方法(聚合函數)簡介的獲取

如果你的MySQL不報錯,說明嚴格模式沒有設置

如何設置

show variables like '%mode%';

set session? 當前窗口有效

set global? 全局有效

set global sql_mode="strict_trans_tables,only_full_group_by";

select * from emp group by post;? # 設置嚴格模式后輸入,會報錯

select id,name from emp group by post;? # 報錯

正確寫法:

select name from emp group by post;

2.獲取每個部門的最高工資

需要用到聚合函數: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;

兩種都可以,推薦使用第一種,結構清晰

每個部門的最低工資

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(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;? # 空字段不行

在統計分組內個數的時候,括號內填寫任意非空字段都可以完成計數

推薦使用能夠唯一標識數據的字段,比如id字段

聚合函數會自動將每一個分組內的單個數據做想要的計算,無需你考慮

3.查詢分組之后的部門名稱和每個部門下所有的學生姓名

group_concat():(分組之后用)能夠拿到分組后每一個數據指定字段(可以是多個)對應的值

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:(不分組時用)拼接字符串達到更好的顯示效果

select concat("NAME: ",name),concat("SAL: ",salary) from emp;

小技巧:

concat就是用來幫你拼接數據的

concat:不分組情況下使用

group_concat:分組之后使用

4.查詢每個員工的年薪

select name,salary*12 from emp;

5.補充

剛開始查詢表,一定要按照最基本的步驟,先確定是哪張表,再確定查這張表有沒有限制條件,再確定是否需要分類,最后再確定需要什么字段對應的信息

你應該將每一步操作產生的結果都當成是一張新的表,然后基于該表再進行其他的操作

聚合函數 max min sum count avg只能在分組之后使用

如果一張表沒有寫group by,默認所有的數據就是一組

6.統計各部門年齡在30歲以上的員工平均工資

先獲取年輕在30歲以上的員工

select * from emp where?age > 30;

再分組求平均工資

select post,avg(salary) from emp where age > 30 group by post;

寫sql語句的時候,一定不要一口氣寫完

前期先按照步驟一步步寫

寫一步查詢看一下結果然后基于當前結果再往后寫

四、having

跟where是一模一樣的,也是用來篩選數據的

但是having是跟在group by之后的

where是對整體數據做一個初步的篩選

而having是對分組之后的數據再進行一次針對性的篩選

1、統計各部門年齡在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;? # 報錯

強調:

having必須在group by后面使用

select * from emp having avg(salary) > 10000;? # 報錯

執行順序

from

where

group by

having

select

五、distinct去重

對重復的數據進行一個去重

去重必須數據是一模一樣的才能去重

只要有一個不一樣,都不能算是的重復的數據

select distinct age from emp;

執行順序

from

where

group by

having

select

distinct

六、order by:排序

默認是升序 asc

select * from emp order by salary;??# 默認升序排

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;

統計各部門年齡在10歲以上的員工平均工資,并且保留平均工資大于1000的部門,然后對平均工資進行排序

select post,avg(salary) from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary);

七、limit:限制展示數據的條數

select * from emp limit 5;? # 只展示數據的五條

select * from emp limit 5,5;

當limit只有一個參數的時候,表示的是只展示幾條

當limit有兩個參數的時候,第一個參數表示的是起始位置,第二個參數表示從起始位置開始往后展示的條數

查詢工資最高的人的詳細信息

(1)先按照薪資排序

(2)再用limit限制,只取一條

select * from emp order by salary desc limit 1;

八、正則

在編程中,只要看到reg開頭的,基本上都是跟正則相關

select * from emp where name regexp '^j.*(n|y)$';

九、多表查詢

1.連表查詢

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 = '技術';

1、內連接:只取兩張表有對應關系的記錄(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 = "技術";

2、左連接: 在內連接的基礎上保留左表沒有對應關系的記錄(left join)

select * from emp left join dep on emp.dep_id = dep.id;

3、右連接: 在內連接的基礎上保留右表沒有對應關系的記錄(right join)

select * from emp right join dep on emp.dep_id = dep.id;

4、全連接:在內連接的基礎上保留左、右面表沒有對應關系的的記錄(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.子查詢

就是將一個查詢語句的結果用括號括起來當作另外一個查詢語句的條件去用

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.* 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

;

可以給表起別名

可以給查詢出來的虛擬表起別名

可以給字段起別名

記住一個規律,表的查詢結果可以作為其他表的查詢條件,也可以通過起別名的方式把它作為一張虛擬表去跟其他表做關聯查詢

總結

以上是生活随笔為你收集整理的mysql分组查询学生平均年龄_8.21MySQL(四)基本查询语句及方法、连表、子查询...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。