mysql 多表連接的


 多表連接查詢的練習

創建所需要的表

-- 創建部門表
CREATE TABLE dept (
    deptno INT PRIMARY KEY,
    dname VARCHAR(15),
    loc VARCHAR(50)
);


-- 導入部門表信息
-- (10,'ACCOUNTING','NEW YORK');
-- (20,'RESEARCH','DALLAS');
-- (30,'SALES','CHICAGO');
-- (40,'OPERATIONS','BOSTON');
INSERT INTO dept VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO dept VALUES (20,'RESEARCH','DALLAS');
INSERT INTO dept VALUES (30,'SALES','CHICAGO');
INSERT INTO dept VALUES (40,'OPERATIONS','BOSTON');

SELECT * FROM dept;

-- 創建工資等級表
CREATE TABLE salgrade (
    grade INT,
    losal INT,
    hisal INT
);

-- 導入工資等級信息
-- (1,700,1200);
-- (2,1201,1400);
-- (3,1401,2000);
-- (4,2001,3000);
-- (5,3001,9999);
INSERT INTO salgrade VALUES (1,700,1200);
INSERT INTO salgrade VALUES (2,1201,1400);
INSERT INTO salgrade VALUES (3,1401,2000);
INSERT INTO salgrade VALUES (4,2001,3000);
INSERT INTO salgrade VALUES (5,3001,9999);

SELECT * FROM salgrade;

  -- 1.查詢各部門經理的詳細信息

-- 1.查詢各部門經理的詳細信息

-- 方法一:笛卡爾積

select  ename , empno,job ,sal
from  emp1,dept
where emp1.deptno = dept.deptno and ename = 'manager';

-- 方法二:交叉連接

select  ename ,empno , job ,sal
from emp1  cross join  dept  on  emp1.deptno = dept.deptno  and  ename = 'manager';


-- 方法三:內連接
select ename ,empno , job , sal
from emp1 inner join dept on emp1.deptno = dept.deptno  and  ename = 'manager';

-- 2.查詢每位員工的工號、姓名、職位、應發工資(sal+comm)和工資等級

select  empno, ename ,job , sal+comm , grade 
from emp1 , salgrade
where  sal +comm between losal and hisal;

# 左連接

select empno ,ename , job ,sal+comm,grade
from emp1 left join salgrade on sal+comm between losal and hisal;


-- 3.查詢所有管理者姓名及其下屬員工姓名(自連接:通過別名,將同一張表視為多張表)

select  e2.name,e1.name
from emp1  as e1 inner join emp1 as e2 on mgr = e2.empno;

-- 4.查詢各部門平均工資和員工數:部門名稱,平均工資和員工數

select avg(sal),count(empno),dname
from dept left join emp1 on dept,feptno = emp1.deptno
group by dept.deptno;


-- 5.查詢各地區員工的平均工齡


select loc,(datediff(now(),hiredate)/365)  平均工齡
from  dept  left join emp1 on dept.deptno= emp1.deptno
group by loc;

  子查詢

-- 標量子查詢:
-- 查詢f_price大於均價的水果信息

select * from fruit having f_price > avg (f_price);

-- 行子查詢
-- 查詢和f_price大於20的水果名稱相同且被同一客戶購買過的的水果信息
# 1、先查詢價格大於20的水果有哪些客戶買過

select  f_name , s_name
from fruit
where f_price >20;

#2、查詢和第一步中計算的到的信息相同的水果

select f_name
from fruit
where (f_name,s_name) in
(select f_name,s_name
from fruit
where f_price >20);


-- 列子查詢:
-- 查詢購買過f_price小於10的客戶購買過的水果信息

#1、先查詢購買過價格小於10的水果的客戶有哪些

select s_name
from fruit
where f_price <10
group by s_name;    

或者
select distinct s_name    #distinct 去除重復值
from fruit
where f_price<10;


#2、在查詢這些客戶購買過那些水果

select  *
from fruit
where  s_name in (
select  distinct s_name
from fruit
where f_price <10);


-- 查詢f_price大於任意f_price在10到20之間的水果信息

# 1、查詢出價格在10到20之間的水果有哪些   # 查詢什么在什么之中的時候用  any

select f_price
from fruit 
where f_price between 10 and 20;


#2、價格大於這些水果的水果信息

select *
from fruit 
where f_price any (
select f_price
from fruit 
where f_price between 10 and 20
);


-- 查詢f_price大於所有f_price在10到20之間的水果信息    全部大於用all
#1、先查詢價格在10到20時間的水果價格
select * from fruit where f_price between 10 and 20;

#2、查詢大於上面值的最大值

select *
from fruit
where f_price >all(select f_price from fruit where f_price between 10 and 20);


-- exists子查詢  ,exists  后面的結構返回(true  ,false)無實際意義
# 查詢是否有大於20的,有則查詢全部,否則截至查詢
select * 
from  fruit
where exists (select * from fruit where f_price > 30);

#注意;  當有大於30的值時執行查詢全部   否則輸出空




-- 表子查詢
-- 查詢在購買了f_price小於20的水果的客戶中購買過三種水果的客戶

select s_name ,count(f_name)
from fruit
where  s_name  in(
select  ditinct s_name
from fruit
where f_price<20
)
group by s_name
having  count(f_name) = 3;


-- 子查詢練習
-- 1.查詢工資高於所有員工平均工資的員工信息

#1、查詢所有員工的平均工資
 
#所有員工平均工資
select avg(sal) from emp1;

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

-- 2.查詢和smith同部門同領導的員工信息

select deptno,mgr
from emp1
where ename = 'smith';

select *
from emp1
where (deptno,mgr) =
	(
	select deptno,mgr
	from emp1
	where ename = 'smith');


-- 3.查詢所有上層管理者的詳細信息
select distinct mgr from emp1;

select *
from emp1
where empno in (select distinct mgr from emp1);
-- 4.工資大於同職位的平均工資的員工信息
select avg(sal) 平均工資,job from emp1 group by job;

select *
from emp1  left join(select avg(sal) 平均工資,job from emp1 group by job) as e on emp1.job=e.job
where emp1.sal > e.平均工資;

-- 5.查詢上層管理者的職位



-- 6.查詢工資等級處於第4的員工姓名及工資

-- 7.每個部門薪水最高的員工信息
select max(sal) 最高,deptno from emp1 group by deptno;

select *
from emp1 left join (select max(sal) 最高,deptno from emp1 group by deptno) a
on emp1.deptno = a.deptno
where emp1.sal = a.最高;

-- 8.工資等級處於第四的員工姓名及工資
select ename,sal,grade
from emp1,salgrade
where sal between losal and hisal and grade = 4;

select ename ,sal
from emp1,(select losal,hisal from salgrade where grade=4) a
where sal between losal and hisal;

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM