MySQL高階查詢簡例

2021-09-07 14:23:42 字數 4215 閱讀 6171

多表查詢

1,內連線

1.1 等值連線

方式一:=

查詢所有員工的員工和部門資訊(查詢員工和部門的資訊)

select * from emp,dept產生乙個笛卡爾集

下面就是避免笛卡爾集的方式:等值條件

select * from emp,dept where emp.deptno = dept.deptno
方式二:join on

select *from emp join deptonemp.deptno = dept.deptno
方式二:inner join on

select 	*from emp inner join dept on emp.deptno = dept.deptno;
1.2 非等值連線

查詢所有員工的姓名和薪資等級資訊

select * from emp,salgrade  where sal between salgrade.losal and  salgrade.hisal
1.3 自連線

查詢每門課程的先修課名稱

select fir.*,sec.cname from course fir,course sec

where fir.cpno = sec.cno

查詢每門課程的間接先修課名稱(先修課的先修課)

select 

fir.cno,fir.cname,third.cno,third.cname

from

course fir,course sec,course third

where

fir.cpno = sec.cno

and

sec.cpno = third.cno

改造:select

fir.cno,fir.cname,third.cno,third.cname

from

course fir

inner join

course sec

on fir.cpno = sec.cno

inner join

course third

on sec.cpno = third.cno

總結:內連線的特點 :求交集

select * from course

select fir.*,sec.cname from course fir,course sec

where fir.cpno = sec.cno

2,外連線

左外連線

查詢所有員工的部門資訊

select 	* from emp left join dept	on	emp.deptno = dept.deptno
右外連線

select 	* from 	emp 	right join 	dept on	emp.deptno = dept.deptno;

select * from emp,dept where emp.deptno(+) = dept.deptno;(oracle中可以如此,)

3,外來鍵

3.1 什麼是外來鍵

本質就是一種約束。
3.2 外來鍵的四個約束

3.3 外來鍵的條件

3.4 建立外來鍵

3.4.1 建表的同時建外鍵

create table emp(

…foreign key(外來鍵字段) references dept(deptno)

)

3.4.2 建立表之後新增外來鍵

alter table emp add foreign key(deptno) references dept(deptno)

3.4.3刪除外來鍵:

語法:alter table 表名稱 drop foreign key 外來鍵名稱;

例:alter table empa drop foreign key empa_ibqk_1;

注意:如果沒有在建表的時候標明外來鍵名稱,可以通過:

show create table 表名 進⾏檢視外來鍵名稱;

4,子查詢

4.1 標量子查詢(子查詢的結果是1行1列)

查詢與egz同部門的員工資訊

select * from emp where deptno = egz的部門號相等

select * from emp where deptno =(select deptno from emp where ename='egz')
4.2 列子查詢(子查詢的結果是n行1列)

查詢與部門10工作崗位相同的員工資訊

select * from emp where empjob in (select empjob from emp where deptno = 10)
4.3 行子查詢(1行n列)

查詢與egz部門和崗位都相同的員工資訊

select * from emp where (deptno,empjob) = (select deptno,empjob from emp where ename='egz')
4.4 錶子查詢(n行n列)

查詢與10號部門相同的崗位和上司的員工資訊

select * from emp where (empjob,mgr) in (select empjob, mgr from emp where deptno = 10)
5,exists 存在

select * from emp where deptno =10 and exists

(select * from dept where emp.deptno = dept.deptno and emp.deptno =20)

6, 派生表:子查詢查詢出來的表,叫虛表 放到from中就叫派生表

select emp.ename,emp.empjob,a.deptno,a.dname

from emp,(select deptno,dname from dept where deptno>10) a

where emp.deptno = a.deptno

7,union 與 union all

select * from emp

union

select * from emp

select * from emp 

union all

select * from emp

select dept.* ,count(dept)from dept, emp;

select * from emp,dept where emp.deptno = dept.deptno;

select dept.deptno, dept.dname,dept.loc,count(empno) from emp ,dept where emp.deptno = dept.deptno group by deptno;

例項的表:

create table dept

( deptno int(2) not null primary key,

dname varchar(14),

loc varchar(13)

)create table emp

( empno int(4) not null,

ename varchar(10),

empjob varchar(9),

mgr int(4),

hiredate date,

sal decimal(7,2),

comm decimal(7,2),

deptno int(2)

)create table salgrade

( grade int not null primary key,

losal decimal(7,2),

hisal decimal(7,2)

)

mysql儲存過程簡例

儲存過程流程 1.drop procedure if exists program weight 刪除 program weight 儲存過程名稱 2.delimiter 預設的輸入的結束符 替換成 建立 create procedure program weight in contentid va...

mysql高階查詢in MySQL高階查詢(一)

in 子查詢 巢狀查詢 重點是思路 為什麼要用in?in 在數值上相當於 但是它可以查詢到更多的符合條件的結果,等於號只可以查詢乙個結果 question 有兩種方法 第一種 使用子查詢替換表連線 使用 inner join 將表與表之間聯動,再將實現條件依次寫出來 第二種 採用子查詢 在where...

mysql 高階 查詢 MYSQL中的高階查詢

1.1.子查詢 1.1.1.在房屋型別中,如何找出比雙人間貴的所有房屋型別?找到雙人間的 根據第一步找到的 作為查詢條件去查滿足條件的房屋型別,利用where字句 子查詢是乙個巢狀在 select insert update 或 delete 語句或其他子查詢中的查詢 子查詢在where語句中的一般...