欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL 復合查詢案例詳解

 更新時間:2025年05月08日 10:08:38   作者:英雄不問出處~  
這篇文章主要介紹了MySQL 復合查詢案例詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧

基本查詢回顧

查詢工資高于500或崗位為MANAGER的雇員,同時還要滿足他們的姓名首字母為大寫的J

select * from emp where (salary>500 or job='MANAGER') and ename like 'J%';
select * from emp where (salary>500 or job='MANAGER') and substring(ename,1,1)='J';

按照部門號升序而雇員的工資降序排序

select * from emp order by deptno asc,sal desc;

使用年薪進行降序排序

select ename,sal,comm,sal*12+ifnull(comm,0) as 年薪 from emp order by 年薪 desc; 

顯示工資最高的員工的名字和工作崗位

select ename,job from emp where sal=(select max(sal) from emp);

顯示工資高于平均工資的員工信息

select * from emp where sal > (select avg(sal) from emp);

顯示每個部門的平均工資和最高工資

// 保留2位精度統(tǒng)計
select deptno,format(avg(sal),2),max(sal) where emp group by deptno;

顯示平均工資低于2000的部門號和它的平均工資

// 分組有多個部門,每個部門的平均工資
// having對分組之后的結(jié)果進行判斷
select deptno,avg(sal) 平均工資 from emp group by deptno having 平均工資 < 2000  

顯示每種崗位的雇員總數(shù),平均工資

select job,count(job) 人數(shù),format(avg(sal),2) 平均工資 from emp group by job;

顯示出每個崗位有多少個人

多表笛卡爾積

兩張表進行組合,第一張表中的第一條信息和第二張表中的4條信息進行組合/窮舉

2. 顯示雇員名、雇員工資以及所在部門的名字

// 連接兩張表進行查詢
select ename,sal,dname from dept,emp where emp.deptno=dept.deptno;

3. 顯示部門號為10的部門名,員工名和工資

select ename,sal,dname,dept.deptno from emp,dept where emp.deptno=dept.deptno and emp.dpetno=10;

4. 顯示各個員工的姓名,工資,及工資級別

select ename,sal,grade,losal,hisal from emp,salgrade where sal between losal and hisal;

自連接 用同一張表進行笛卡爾積,但是要進行重命名

顯示員工FORD的上級領(lǐng)導的編號和姓名(mgr是員工領(lǐng)導的編號–empno)
先找FORD的領(lǐng)導編號
根據(jù)領(lǐng)導編號找領(lǐng)導的信息

select ename,empno from emp where empno=(select mgr from emp where ename='FORD');

select e2.ename,e2.empno from emp e1,emp e2 where e1.ename='FORD' and e1.mgr=e2.empno;

子查詢與where

子查詢

1. 子查詢是指嵌入在其他sql語句中的select語句,也叫嵌套查詢

單行子查詢 返回一行記錄的子查詢
顯示SMITH同一部門的員工

select * from emp where deptno=(select deptno from emp where ename='SMITH'); 

多行子查詢

返回多行記錄的子查詢in關(guān)鍵字,判斷集合是否在列當中
查詢和10號部門的工作崗位相同的雇員的名字,崗位,工資,部門號,但是不包含10自己的

select ename,job,sal,deptno from emp where job in(select distinct job from where deptno=10) and
deptno <> 10;

并且知道對應的員工屬于哪個部門的名字

select ename,job,sal,dname from
(select ename,job,sal,deptno from emp where job in 
(select distinct job from emp where deptno=10) and deptno<>10;)
and deptno <>10) as tmp ,dept where dept.deptno=tmp.deptno;

4. all關(guān)鍵字;顯示工資比部門30的所有員工的工資高的員工的姓名、工資和部門號

select * from emp where sal > (select max(sal) from emp where deptno=30);

all,比所有人的工資都高,就是比30號部門最高工資都高

select * from emp where sal > any(select distinct sal from emp where deptno=30);

5. any關(guān)鍵字;顯示工資比部門30的任意員工的工資高的員工的姓名、工資和部門號(包含自己部門的員工)
任意指的是,比該部門中最小的大都行

select * from emp where sal > any(select distinct sal from emp where deptno=30);

多列子查詢

單行子查詢是指子查詢只返回單列,單行數(shù)據(jù);多行子查詢是指返回單列多行數(shù)據(jù),都是針對單列而言的,而多列子查詢則是指查詢返回多個列數(shù)據(jù)的子查詢語句

案例,查詢和SMITH的部門和崗位完全相同的所有雇員,不含SMITH本人

select * from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH) and ename <> 'SMITH'; 

總結(jié):

  • 目前全部的子查詢,全部都在where子句中,充當判斷條件
  • 任何時候查詢出來的邏輯結(jié)構(gòu),本質(zhì)上在邏輯上也是表結(jié)構(gòu)
  • 子查詢不僅可以出現(xiàn)在where后面,也可以出現(xiàn)在from后面 (把子查詢當成一個臨時表使用)

子查詢與from

子查詢語句出現(xiàn)在from子句中。這里要用到數(shù)據(jù)查詢的技巧,把一個子查詢當做一個臨時表使用。

案例
顯示每個高于自己部門平均工資的員工的姓名、部門、工資、平均工資

// 查詢分組后的部門號和平均工資
select deptno,avg(sal) from emp group by dpetno;

// 對員工表和部門的平均工資表做拼接
// 做笛卡爾積要部門號相同才有意義
select * from emp,(select deptno,avg(sal) from emp group by deptno) as tmp where emp.deptno=tmp.deptno;

select * from emp,(select deptno,avg(sal) as myavg from emp group by deptno) as tmp where emp.deptno=tmp.deptno and emp.sal > tmp.myavg;

在加一個條件,篩選出部門號和辦公地點,要連接部門表和這個臨時表

2. 查找每個部門工資最高的人的姓名、工資、部門、最高工資

select ename,sal,t1.deptno,mymax from emp as t1,(select deptno,max(sal) mymax from emp group by deptno) as t2 where t1.deptno=t2.deptno and t1.sal=t2.mymax;

3. 顯示每個部門的信息(部門名,編號,地址)(dept)和人員數(shù)量

select t1.deptno,t1.dname,t1.loc,t2.dept_num from dept as t1,(select deptno,count(*) as dept_num from emp group by deptno) as t2 where t1.deptno=t2.deptno;

4. COUNT(*) 是聚合函數(shù),所以不需要在 GROUP BY 里出現(xiàn)。
5. 在標準 SQL(如 MySQL 5.7+、PostgreSQL、SQL Server)中,SELECT 子句中的 所有非聚合列(如 DEPT.dname, DEPT.loc)必須出現(xiàn)在 GROUP BY 子句中,否則會報錯:
6. group by 要符合語法

使用多表(不推薦使用)

select DEPT.dname, DEPT.deptno, DEPT.loc,count(*) '部門人數(shù)' from EMP,
DEPT
where EMP.deptno=DEPT.deptno
group by DEPT.deptno,DEPT.dname,DEPT.loc;

總結(jié)

合并查詢(不太重要)

合并多個select的執(zhí)行結(jié)果,可以使用集合操作符 union,union all union 該操作符用于取得兩個結(jié)果集的并集。當使用該操作符時,會自動去掉結(jié)果集中的重復行。使用union和all union 必須保證列的數(shù)量是相同的

案例
將工資大于2500或職位是MANAGER的人找出來

select * from emp where sal>2500 union select * from emp where job='MANAGER'; 

去重掉了重復項

union

all 效果和union一樣,但是不會去掉重復的信息

案例:

將工資大于25000或職位是MANAGER的人找出來

select * from emp where sal > 25000 union select * from emp where job='MANAGER';

到此這篇關(guān)于MySQL 復合查詢的文章就介紹到這了,更多相關(guān)mysql 復合查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論