首頁技術(shù)文章正文

java基礎(chǔ)-多表查詢

更新時間:2018-09-17 來源:黑馬程序員javaEE培訓(xùn)學(xué)院 瀏覽量:

多表查詢:

    * 查詢語法:

        select

            列名列表

        from

            表名列表

        where.... * 準(zhǔn)備sql

        # 創(chuàng)建部門表

        CREATE TABLE dept(

            id INT PRIMARY KEY AUTO_INCREMENT,

            NAME VARCHAR(20)

        );

        INSERT INTO dept (NAME) VALUES ('開發(fā)部'),('市場部'),('財務(wù)部');

        # 創(chuàng)建員工表

        CREATE TABLE emp (

            id INT PRIMARY KEY AUTO_INCREMENT,

            NAME VARCHAR(10),

            gender CHAR(1), -- 性別

            salary DOUBLE, -- 工資

            join_date DATE, -- 入職日期

            dept_id INT,

            FOREIGN KEY (dept_id) REFERENCES dept(id) -- 外鍵,關(guān)聯(lián)部門表(部門表的主鍵)

        );

        INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('孫悟空','男',7200,'2013-02-24',1);

        INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('豬八戒','男',3600,'2010-12-02',2);

        INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('唐僧','男',9000,'2008-08-08',2);

        INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('白骨精','女',5000,'2015-10-07',3);

        INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('蜘蛛精','女',4500,'2011-03-14',1); * 笛卡爾積:

        * 有兩個集合A,B .取這兩個集合的所有組成情況。

        * 要完成多表查詢,需要消除無用的數(shù)據(jù)

    * 多表查詢的分類:

        1. 內(nèi)連接查詢:

            1. 隱式內(nèi)連接:使用where條件消除無用數(shù)據(jù)

                * 例子:

                -- 查詢所有員工信息和對應(yīng)的部門信息

 

                SELECT * FROM emp,dept WHERE emp.`dept_id` = dept.`id`;

                

                -- 查詢員工表的名稱,性別。部門表的名稱

                SELECT emp.name,emp.gender,dept.name FROM emp,dept WHERE emp.`dept_id` = dept.`id`;

                

                SELECT

                    t1.name, -- 員工表的姓名

                    t1.gender,-- 員工表的性別

                    t2.name -- 部門表的名稱

                FROM

                    emp t1,

                    dept t2

                WHERE

                    t1.`dept_id` = t2.`id`;

 

    

            2. 顯式內(nèi)連接:

                * 語法: select 字段列表 from 表名1 [inner] join 表名2 on 條件

                * 例如:

                    * SELECT * FROM emp INNER JOIN dept ON emp.`dept_id` = dept.`id`;    

                    * SELECT * FROM emp JOIN dept ON emp.`dept_id` = dept.`id`;    

 

            3. 內(nèi)連接查詢:

                1. 從哪些表中查詢數(shù)據(jù)

                2. 條件是什么

                3. 查詢哪些字段

        2. 外鏈接查詢:

            1. 左外連接:

                * 語法:select 字段列表 from 表1 left [outer] join 表2 on 條件;

                * 查詢的是左表所有數(shù)據(jù)以及其交集部分。

                * 例子:

                    -- 查詢所有員工信息,如果員工有部門,則查詢部門名稱,沒有部門,則不顯示部門名稱

                    SELECT     t1.*,t2.`name` FROM emp t1 LEFT JOIN dept t2 ON t1.`dept_id` = t2.`id`;

            2. 右外連接:

                * 語法:select 字段列表 from 表1 right [outer] join 表2 on 條件;

                * 查詢的是右表所有數(shù)據(jù)以及其交集部分。

                * 例子:

                    SELECT     * FROM dept t2 RIGHT JOIN emp t1 ON t1.`dept_id` = t2.`id`;

        3. 子查詢:

            * 概念:查詢中嵌套查詢,稱嵌套查詢?yōu)樽硬樵儭?/p>

                -- 查詢工資最高的員工信息

                -- 1 查詢最高的工資是多少 9000

                SELECT MAX(salary) FROM emp;

                

                -- 2 查詢員工信息,并且工資等于9000的

                SELECT * FROM emp WHERE emp.`salary` = 9000;

                

                -- 一條sql就完成這個操作。子查詢

                SELECT * FROM emp WHERE emp.`salary` = (SELECT MAX(salary) FROM emp);

 

            * 子查詢不同情況

                1. 子查詢的結(jié)果是單行單列的:

                    * 子查詢可以作為條件,使用運算符去判斷。 運算符: > >= < <= =

                    *

                    -- 查詢員工工資小于平均工資的人

                    SELECT * FROM emp WHERE emp.salary < (SELECT AVG(salary) FROM emp);

                2. 子查詢的結(jié)果是多行單列的:

                    * 子查詢可以作為條件,使用運算符in來判斷

                    -- 查詢'財務(wù)部'和'市場部'所有的員工信息

                    SELECT id FROM dept WHERE NAME = '財務(wù)部' OR NAME = '市場部';

                    SELECT * FROM emp WHERE dept_id = 3 OR dept_id = 2;

                    -- 子查詢

                    SELECT * FROM emp WHERE dept_id IN (SELECT id FROM dept WHERE NAME = '財務(wù)部' OR NAME = '市場部');

 

                3. 子查詢的結(jié)果是多行多列的:

                    * 子查詢可以作為一張?zhí)摂M表參與查詢

                    -- 查詢員工入職日期是2011-11-11日之后的員工信息和部門信息

                    -- 子查詢

                    SELECT * FROM dept t1 ,(SELECT * FROM emp WHERE emp.`join_date` > '2011-11-11') t2

                    WHERE t1.id = t2.dept_id;

                    

                    -- 普通內(nèi)連接

                    SELECT * FROM emp t1,dept t2 WHERE t1.`dept_id` = t2.`id` AND t1.`join_date` >  '2011-11-11'

 

        * 多表查詢練習(xí)

 

                -- 部門表

                CREATE TABLE dept (

                  id INT PRIMARY KEY PRIMARY KEY, -- 部門id

                  dname VARCHAR(50), -- 部門名稱

                  loc VARCHAR(50) -- 部門所在地

                );

                

                -- 添加4個部門

                INSERT INTO dept(id,dname,loc) VALUES

                (10,'教研部','北京'),

                (20,'學(xué)工部','上海'),

                (30,'銷售部','廣州'),

                (40,'財務(wù)部','深圳');

                

                

                

                -- 職務(wù)表,職務(wù)名稱,職務(wù)描述

                CREATE TABLE job (

                  id INT PRIMARY KEY,

                  jname VARCHAR(20),

                  description VARCHAR(50)

                );

                

                -- 添加4個職務(wù)

                INSERT INTO job (id, jname, description) VALUES

                (1, '董事長', '管理整個公司,接單'),

                (2, '經(jīng)理', '管理部門員工'),

                (3, '銷售員', '向客人推銷產(chǎn)品'),

                (4, '文員', '使用辦公軟件');

                

                

                

                -- 員工表

                CREATE TABLE emp (

                  id INT PRIMARY KEY, -- 員工id

                  ename VARCHAR(50), -- 員工姓名

                  job_id INT, -- 職務(wù)id

                  mgr INT , -- 上級領(lǐng)導(dǎo)

                  joindate DATE, -- 入職日期

                  salary DECIMAL(7,2), -- 工資

                  bonus DECIMAL(7,2), -- 獎金

                  dept_id INT, -- 所在部門編號

                  CONSTRAINT emp_jobid_ref_job_id_fk FOREIGN KEY (job_id) REFERENCES job (id),

                  CONSTRAINT emp_deptid_ref_dept_id_fk FOREIGN KEY (dept_id) REFERENCES dept (id)

                );

                

                -- 添加員工

                INSERT INTO emp(id,ename,job_id,mgr,joindate,salary,bonus,dept_id) VALUES

                (1001,'孫悟空',4,1004,'2000-12-17','8000.00',NULL,20),

                (1002,'盧俊義',3,1006,'2001-02-20','16000.00','3000.00',30),

                (1003,'林沖',3,1006,'2001-02-22','12500.00','5000.00',30),

                (1004,'唐僧',2,1009,'2001-04-02','29750.00',NULL,20),

                (1005,'李逵',4,1006,'2001-09-28','12500.00','14000.00',30),

                (1006,'宋江',2,1009,'2001-05-01','28500.00',NULL,30),

                (1007,'劉備',2,1009,'2001-09-01','24500.00',NULL,10),

                (1008,'豬八戒',4,1004,'2007-04-19','30000.00',NULL,20),

                (1009,'羅貫中',1,NULL,'2001-11-17','50000.00',NULL,10),

                (1010,'吳用',3,1006,'2001-09-08','15000.00','0.00',30),

                (1011,'沙僧',4,1004,'2007-05-23','11000.00',NULL,20),

                (1012,'李逵',4,1006,'2001-12-03','9500.00',NULL,30),

                (1013,'小白龍',4,1004,'2001-12-03','30000.00',NULL,20),

                (1014,'關(guān)羽',4,1007,'2002-01-23','13000.00',NULL,10);

                

                

                

                -- 工資等級表

                CREATE TABLE salarygrade (

                  grade INT PRIMARY KEY,   -- 級別

                  losalary INT,  -- 最低工資

                  hisalary INT -- 最高工資

                );

                

                -- 添加5個工資等級

                INSERT INTO salarygrade(grade,losalary,hisalary) VALUES

                (1,7000,12000),

                (2,12010,14000),

                (3,14010,20000),

                (4,20010,30000),

                (5,30010,99990);

                

                -- 需求:

                

                -- 1.查詢所有員工信息。查詢員工編號,員工姓名,工資,職務(wù)名稱,職務(wù)描述

                /*

                    分析:

                        1.員工編號,員工姓名,工資,需要查詢emp表  職務(wù)名稱,職務(wù)描述 需要查詢job表

                        2.查詢條件 emp.job_id = job.id

                

                */

                SELECT

                    t1.`id`, -- 員工編號

                    t1.`ename`, -- 員工姓名

                    t1.`salary`,-- 工資

                    t2.`jname`, -- 職務(wù)名稱

                    t2.`description` -- 職務(wù)描述

                FROM

                    emp t1, job t2

                WHERE

                    t1.`job_id` = t2.`id`;

                

                

                

                -- 2.查詢員工編號,員工姓名,工資,職務(wù)名稱,職務(wù)描述,部門名稱,部門位置

                /*

                    分析:

                        1. 員工編號,員工姓名,工資 emp  職務(wù)名稱,職務(wù)描述 job  部門名稱,部門位置 dept

                        2. 條件: emp.job_id = job.id and emp.dept_id = dept.id

                */

                

                SELECT

                    t1.`id`, -- 員工編號

                    t1.`ename`, -- 員工姓名

                    t1.`salary`,-- 工資

                    t2.`jname`, -- 職務(wù)名稱

                    t2.`description`, -- 職務(wù)描述

                    t3.`dname`, -- 部門名稱

                    t3.`loc` -- 部門位置

                FROM

                    emp t1, job t2,dept t3

                WHERE

                    t1.`job_id` = t2.`id` AND t1.`dept_id` = t3.`id`;

                   

                -- 3.查詢員工姓名,工資,工資等級

                /*

                    分析:

                        1.員工姓名,工資 emp  工資等級 salarygrade

                        2.條件 emp.salary >= salarygrade.losalary and emp.salary <= salarygrade.hisalary

                            emp.salary BETWEEN salarygrade.losalary and salarygrade.hisalary

                */

                SELECT

                    t1.ename ,

                    t1.`salary`,

                    t2.*

                FROM emp t1, salarygrade t2

                WHERE t1.`salary` BETWEEN t2.`losalary` AND t2.`hisalary`;

                

                

                

                -- 4.查詢員工姓名,工資,職務(wù)名稱,職務(wù)描述,部門名稱,部門位置,工資等級

                /*

                    分析:

                        1. 員工姓名,工資 emp , 職務(wù)名稱,職務(wù)描述 job 部門名稱,部門位置,dept  工資等級 salarygrade

                        2. 條件: emp.job_id = job.id and emp.dept_id = dept.id and emp.salary BETWEEN salarygrade.losalary and salarygrade.hisalary

                            

                */

                SELECT

                    t1.`ename`,

                    t1.`salary`,

                    t2.`jname`,

                    t2.`description`,

                    t3.`dname`,

                    t3.`loc`,

                    t4.`grade`

                FROM

                    emp t1,job t2,dept t3,salarygrade t4

                WHERE

                    t1.`job_id` = t2.`id`

                    AND t1.`dept_id` = t3.`id`

                    AND t1.`salary` BETWEEN t4.`losalary` AND t4.`hisalary`;

                

                

                

                -- 5.查詢出部門編號、部門名稱、部門位置、部門人數(shù)

                

                /*

                    分析:

                        1.部門編號、部門名稱、部門位置 dept 表。 部門人數(shù) emp表

                        2.使用分組查詢。按照emp.dept_id完成分組,查詢count(id)

                        3.使用子查詢將第2步的查詢結(jié)果和dept表進(jìn)行關(guān)聯(lián)查詢

                        

                */

                SELECT

                    t1.`id`,t1.`dname`,t1.`loc` , t2.total

                FROM

                    dept t1,

                    (SELECT

                        dept_id,COUNT(id) total

                    FROM

                        emp

                    GROUP BY dept_id) t2

                WHERE t1.`id` = t2.dept_id;

                

                

                -- 6.查詢所有員工的姓名及其直接上級的姓名,沒有領(lǐng)導(dǎo)的員工也需要查詢

                

                /*

                    分析:

                        1.姓名 emp, 直接上級的姓名 emp

                            * emp表的id 和 mgr 是自關(guān)聯(lián)

                        2.條件 emp.id = emp.mgr

                        3.查詢左表的所有數(shù)據(jù),和 交集數(shù)據(jù)

                            * 使用左外連接查詢

                    

                */

                /*

                select

                    t1.ename,

                    t1.mgr,

                    t2.`id`,

                    t2.ename

                from emp t1, emp t2

                where t1.mgr = t2.`id`;

                

                */

                

                SELECT

                    t1.ename,

                    t1.mgr,

                    t2.`id`,

                    t2.`ename`

                FROM emp t1

                LEFT JOIN emp t2

                ON t1.`mgr` = t2.`id`;

本文版權(quán)歸黑馬程序員JavaEE學(xué)院所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請注明作者出處。謝謝!
作者:黑馬程序員javaEE培訓(xùn)學(xué)院
首發(fā):http://java.itheima.com/


分享到:
在線咨詢 我要報名
和我們在線交談!