您好,登錄后才能下訂單哦!
本文主要給大家舉例介紹mysql多表查詢方法,文章內(nèi)容都是筆者用心摘選和編輯的,具有一定的針對(duì)性,對(duì)大家的參考意義還是比較大的,下面跟筆者一起了解下mysql多表查詢方法吧。
創(chuàng)建示例數(shù)據(jù)表:
#學(xué)生表 create table stu(sid int primary key,sname varchar(10) not null); #成績(jī)表 create table score (sid int, score int , cid int); #科目表 create table subjects (cid int primary key,cname varchar(10)); alter table score add constraint fk_score_sid foreign key(sid) references stu(sid); alter table score add constraint fk_score_cid foreign key(cid) references subjects(cid);
查詢 學(xué)生成績(jī) 和科目名稱
select * from stu,score where stu.sid = score.sid;
查詢學(xué)生的成績(jī) 要求70以上的
select s.sid, s.sname, c.score from stu s,score c where s.sid = c.sid and c.score>70;
查詢所有學(xué)生的成績(jī)
select * from stu s inner join score c on s.sid = c.sid; 內(nèi)連接相當(dāng)于把兩張表鏈接成一張表查詢 使用inner join 關(guān)鍵詞 條件 使用on 并且Inner 可以省略 外鏈接查詢 左外連接查詢 以左邊的表為主 左邊表中的數(shù)據(jù)都會(huì)查詢出來 有可能會(huì)產(chǎn)生無用的數(shù)據(jù) select s.sid, s.sname, c.score from score c left join stu s on s.sid = c.sid; 右外鏈接查詢 select s.sid, s.sname, c.score from score c right join stu s on s.sid = c.sid; 自然鏈接 :會(huì)自動(dòng)匹配表中相同的字段 沒有外鍵也能自然查詢 select * from stu natural join score;
查詢所有學(xué)生的分?jǐn)?shù)和考試科目
select s.sid, s.sname, c.score,u.cname from stu s left join score c on s.sid = c.sid join subjects u on c.cid = u.cid; 三張表查詢時(shí)2條件 n張表n-1條件
創(chuàng)建示例數(shù)據(jù)表:
#員工表 CREATE TABLE emp( empno INT, ename VARCHAR(50), job VARCHAR(50), mgr INT, hiredate DATE, sal DECIMAL(7,2), comm decimal(7,2), deptno INT ); INSERT INTO emp values(7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20); INSERT INTO emp values(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30); INSERT INTO emp values(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30); INSERT INTO emp values(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20); INSERT INTO emp values(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30); INSERT INTO emp values(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30); INSERT INTO emp values(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10); INSERT INTO emp values(7788,'SCOTT','ANALYST',7566,'1987-04-19',3000,NULL,20); INSERT INTO emp values(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10); INSERT INTO emp values(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30); INSERT INTO emp values(7876,'ADAMS','CLERK',7788,'1987-05-23',1100,NULL,20); INSERT INTO emp values(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30); INSERT INTO emp values(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20); INSERT INTO emp values(7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10); -- 部門表:dept CREATE TABLE dept( deptno INT, dname varchar(14), loc varchar(13) ); 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');
查詢與 SCOTT 同一個(gè)部門的員工
select ename from emp where deptno = (select deptno from emp where ename=’SCOTT’);
工資高于30號(hào)部門所有人的員工信息
select * from emp where sal>(select max(sal) from emp where deptno = 30);
查詢工作和工資與MARTIN 完全相同的員工信息
select * from emp where (job,sal) in (select job,sal from emp where ename=’MARTIN’);
有兩個(gè)以上有直接下屬的員工信息
select * from emp where empno in (select mgr from emp group by mgr having count(mgr)>=2);
查詢員工編號(hào)為7788的員工名稱 員工工資 部門名稱 部門地址
select e.ename,e.sal,d.dname,d.loc from emp e ,dept d where e.deptno = d.deptno and empno = 7788;
求7369員工編號(hào)、姓名、經(jīng)理編號(hào)和經(jīng)理姓名
select ename,empno from emp where empno in (7369, (select mgr from emp where empno = 7369)); select e1.ename,e1.empno,e2.ename,e2.empno from emp e1,emp e2 where e1.mgr = e2.empno and e1.empno = 7369
求各個(gè)部門薪水最高的員工所有信息
select * from emp where (deptno ,sal) in (select deptno,max(sal) from emp group by deptno); 查詢過程中出現(xiàn)問題 查詢時(shí)沒有把部門的條件帶進(jìn)去 只是按最高工資處理的 可以把查詢結(jié)果返回的結(jié)果集當(dāng)做一個(gè)新表來使用 select * from emp e1 ,(select deptno,max(sal) msal from emp group by deptno) e2 where e1.sal = e2.msal and e1.deptno = e2.deptno;
看完以上關(guān)于mysql多表查詢方法,很多讀者朋友肯定多少有一定的了解,如需獲取更多的行業(yè)知識(shí)信息 ,可以持續(xù)關(guān)注我們的行業(yè)資訊欄目的。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。