1. 程式人生 > >內連線:INNER JOIN

內連線:INNER JOIN

資料準備

1.員工表(tb_emp)

CREATE TABLE `tb_emp` (
  `employee_id` int(6) NOT NULL AUTO_INCREMENT COMMENT '員工編號',
  `first_name` varchar(20) DEFAULT NULL COMMENT '名',
  `last_name` varchar(25) DEFAULT NULL COMMENT '性',
  `email` varchar(25) DEFAULT NULL COMMENT '郵箱',
  `phone_number` varchar(20) DEFAULT
NULL COMMENT '電話號碼', `job_id` varchar(10) DEFAULT NULL COMMENT '工種id', `salary` double(10,2) DEFAULT NULL COMMENT '工資', `commission_pct` double(4,2) DEFAULT NULL COMMENT '獎金率', `manager_id` int(6) DEFAULT NULL COMMENT '領導編號', `department_id` int(4) DEFAULT NULL COMMENT '部門編號', `hiredate` datetime
DEFAULT NULL COMMENT '入職日期', PRIMARY KEY (`employee_id`) ) ENGINE=InnoDB AUTO_INCREMENT=207 DEFAULT CHARSET=gb2312;

2.部門表(tb_emp)

CREATE TABLE `tb_dep` (
  `department_id` int(4) NOT NULL AUTO_INCREMENT COMMENT '部門編號',
  `department_name` varchar(3) DEFAULT NULL COMMENT '部門名稱',
  `manager_id`
int(6) DEFAULT NULL COMMENT '部門領導編號', `location_id` int(4) DEFAULT NULL COMMENT '位置編號', PRIMARY KEY (`department_id`) ) ENGINE=InnoDB AUTO_INCREMENT=271 DEFAULT CHARSET=gb2312;

3.工種表(tb_job)

CREATE TABLE `tb_job` (
  `job_id` varchar(10) NOT NULL COMMENT '工種編號',
  `job_title` varchar(35) DEFAULT NULL COMMENT '工種名稱',
  `min_salary` int(6) DEFAULT NULL COMMENT '最低工資',
  `max_salary` int(6) DEFAULT NULL COMMENT '最高工資',
  PRIMARY KEY (`job_id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

4.工資等級表

CREATE TABLE `tb_job_grade` (
  `grade_level` varchar(3) DEFAULT NULL COMMENT '工資等級',
  `lowest_sal` int(11) DEFAULT NULL COMMENT '等級的最小工資',
  `highest_sal` int(11) DEFAULT NULL COMMENT '等級的最大工資'
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

1.等值內連線

1.1兩張表的等值連線

需求:查詢部門的員工個數大於5的部門名稱和該部門的員工個數(coun),並按照員工個數按照降序取Top3.

mysql> SELECT department_name,count(1) coun 
       FROM tb_emp e 
       INNER JOIN 
       tb_dep d 
       ON e.department_id = d.department_id 
       GROUP BY department_name 
       HAVING coun>5 
       ORDER BY coun DESC 
       LIMIT 3;
+-----------------+------+
| department_name | coun |
+-----------------+------+
| Shi             |   45 |
| Sal             |   34 |
| Fin             |    6 |
+-----------------+------+
3 rows in set
1.2.多表的等值連線

多張表的連線使用多個inner join 連線即可;
需求:查詢員工名、部門名、工種名,並且按照員工名稱升序取Top5;
分析:
1.員工名、部門名、工種名分寫來自於tb_emp、tb_dep、tb_job,那麼需要將這三張表進行連線
2.找出連線條件
3.升序order by asc ,top5即limit 5;

mysql> SELECT last_name,department_name,job_title 
       FROM tb_emp e 
       INNER JOIN tb_dep d 
       ON e.department_id=d.department_id 
       INNER JOIN tb_job j ON e.job_id=j.job_id 
       ORDER BY last_name ASC 
       LIMIT 5;
+-----------+-----------------+---------------------------------+
| last_name | department_name | job_title                       |
+-----------+-----------------+---------------------------------+
| Abel      | Sal             | Sales Representative            |
| Ande      | Sal             | Sales Representative            |
| Atkinson  | Shi             | Stock Clerk                     |
| Austin    | IT              | Programmer                      |
| Baer      | Pub             | Public Relations Representative |
+-----------+-----------------+---------------------------------+
5 rows in se

2.非等值內連線

需求:查詢工資等級的個數(coun)大於30的等級名稱和個數,並按照個數降序取TOP3
需求分析:
1.等級的個數、等級名稱:需要將tb_emptb_job_grade進行連線;
2.連線條件:根據工資等級表的各個等級的工資範圍判斷各個員工的工資在哪一個等級;
3.等級的個數:需要進行分組,並且分組的條件是等級名稱;
4.個數降序:order by desc
5.取出top3:limit 3。

mysql> SELECT COUNT(1) coun ,grade_level 
       FROM tb_emp e 
       JOIN tb_job_grade g 
       ON e.salary BETWEEN g.lowest_sal AND g.highest_sal 
       GROUP BY grade_level 
       HAVING coun > 30 
       ORDER BY coun 
       DESC LIMIT 3;
+------+-------------+
| coun | grade_level |
+------+-------------+
|   38 | C           |
+------+-------------+
1 row in set

3.自連線

需求:查詢員工名、以及該員工對應的上級名稱
需求分析:
1.員工名和上級名稱對來自於tb_emp;
2.連線的條件:員工表1的上級id等於員工表2的員工id

mysql> SELECT e1.last_name ,e2.last_name 
       FROM tb_emp e1 
       JOIN tb_emp e2 ON e1.manager_id = e2.employee_id 
       limit 5; 
+-----------+-----------+
| last_name | last_name |
+-----------+-----------+
| Kochhar   | K_ing     |
| De Haan   | K_ing     |
| Hunold    | De Haan   |
| Ernst     | Hunold    |
| Austin    | Hunold    |
+-----------+--

在sql92中自連線的寫法如下:

mysql> SELECT e1.last_name ,e2.last_name
       FROM tb_emp e1,tb_emp e2 
       WHERE e1.manager_id = e2.employee_id 
       limit 5; 
+-----------+-----------+
| last_name | last_name |
+-----------+-----------+
| Kochhar   | K_ing     |
| De Haan   | K_ing     |
| Hunold    | De Haan   |
| Ernst     | Hunold    |
| Austin    | Hunold    |
+-----------+-----------+
5 rows in set

last edit date:2018-11-18
location:廣州-海珠-西畔裡