SQL_7种JOIN理论与实例

image


实例

现在有两张表,分别是employee表和dept

mysql> select * from employee;
+----+---------------+---------+
| id | employee_name | dept_id |
+----+---------------+---------+
|  1 | zhangsan      |       1 |
|  2 | lisi          |       3 |
|  3 | wangwu        |       2 |
|  4 | zhaoliu       |       4 |
|  5 | liuqi         |     100 |
+----+---------------+---------+
5 rows in set (0.00 sec)

mysql> select * from dept;
+---------+-----------+
| dept_id | dept_name |
+---------+-----------+
|       1 | 人事部    |
|       2 | 测试部    |
|       3 | 生产部    |
|       4 | 研发部    |
|       5 | 运营部    |
|       6 | 财务部    |
+---------+-----------+

Smiley face

#理论:
SELECT
	< select_list >
FROM
	tableA A
INNER JOIN tableB B ON A.key = B.key

#实例sql:
SELECT * FROM `employee` INNER JOIN `dept` ON `employee`.dept_id = `dept`.dept_id;
+----+---------------+---------+---------+-----------+
| id | employee_name | dept_id | dept_id | dept_name |
+----+---------------+---------+---------+-----------+
|  1 | zhangsan      |       1 |       1 | 人事部    |
|  2 | lisi          |       3 |       3 | 生产部    |
|  3 | wangwu        |       2 |       2 | 测试部    |
|  4 | zhaoliu       |       4 |       4 | 研发部    |
+----+---------------+---------+---------+-----------+

Smiley face
#理论:
SELECT
	< select_list >
FROM
	tableA A
LEFT JOIN tableB B ON A.key = B.key

#实例sql:
SELECT * FROM `employee` LEFT JOIN `dept` ON `employee`.dept_id = `dept`.dept_id;
+----+---------------+---------+---------+-----------+
| id | employee_name | dept_id | dept_id | dept_name |
+----+---------------+---------+---------+-----------+
|  1 | zhangsan      |       1 |       1 | 人事部    |
|  2 | lisi          |       3 |       3 | 生产部    |
|  3 | wangwu        |       2 |       2 | 测试部    |
|  4 | zhaoliu       |       4 |       4 | 研发部    |
|  5 | liuqi         |     100 |    NULL | NULL      |
+----+---------------+---------+---------+-----------+

Smiley face
#理论:
SELECT
	< select_list >
FROM
	tableA A
RIGHT JOIN tableB B ON A.key = B.key

#实例sql:
SELECT * FROM `employee` RIGHT JOIN `dept` ON `employee`.dept_id = `dept`.dept_id;
+------+---------------+---------+---------+-----------+
| id   | employee_name | dept_id | dept_id | dept_name |
+------+---------------+---------+---------+-----------+
|    1 | zhangsan      |       1 |       1 | 人事部    |
|    2 | lisi          |       3 |       3 | 生产部    |
|    3 | wangwu        |       2 |       2 | 测试部    |
|    4 | zhaoliu       |       4 |       4 | 研发部    |
| NULL | NULL          |    NULL |       5 | 运营部    |
| NULL | NULL          |    NULL |       6 | 财务部    |
+------+---------------+---------+---------+-----------+

Smiley face
#理论:
SELECT
	< select_list >
FROM
	tableA A
LEFT JOIN tableB B ON A.key = B.key
WHERE B.key IS NULL;

#实例sql:
SELECT * FROM `employee` LEFT JOIN `dept` ON `employee`.dept_id = `dept`.dept_id WHERE `dept`.dept_id IS NULL;
+----+---------------+---------+---------+-----------+
| id | employee_name | dept_id | dept_id | dept_name |
+----+---------------+---------+---------+-----------+
|  5 | liuqi         |     100 |    NULL | NULL      |
+----+---------------+---------+---------+-----------+

Smiley face
#理论:
SELECT
	< select_list >
FROM
	tableA A
RIGHT JOIN tableB B ON A.key = B.key
WHERE A.key IS NULL

#实例sql:
SELECT * FROM `employee` RIGHT JOIN `dept` ON `employee`.dept_id = `dept`.dept_id WHERE `employee`.dept_id IS NULL;
+------+---------------+---------+---------+-----------+
| id   | employee_name | dept_id | dept_id | dept_name |
+------+---------------+---------+---------+-----------+
| NULL | NULL          |    NULL |       5 | 运营部    |
| NULL | NULL          |    NULL |       6 | 财务部    |
+------+---------------+---------+---------+-----------+

Smiley face
#理论:
SELECT
	< select_list >
FROM
	tableA A
FULL OUTER JOIN tableB B ON A.key = B.key

#实例:
SELECT * FROM `employee` LEFT JOIN `dept` ON `employee`.dept_id = `dept`.dept_id
UNION
SELECT * FROM `employee` RIGHT JOIN `dept` ON `employee`.dept_id = `dept`.dept_id;
+------+---------------+---------+---------+-----------+
| id   | employee_name | dept_id | dept_id | dept_name |
+------+---------------+---------+---------+-----------+
|    1 | zhangsan      |       1 |       1 | 人事部    |
|    2 | lisi          |       3 |       3 | 生产部    |
|    3 | wangwu        |       2 |       2 | 测试部    |
|    4 | zhaoliu       |       4 |       4 | 研发部    |
|    5 | liuqi         |     100 |    NULL | NULL      |
| NULL | NULL          |    NULL |       5 | 运营部    |
| NULL | NULL          |    NULL |       6 | 财务部    |
+------+---------------+---------+---------+-----------+

Smiley face
#理论:
SELECT
	< select_list >
FROM
	tableA A
FULL OUTER JOIN tableB B ON A.key = B.key
WHERE A.key IS NULL OR B.key IS NULL

#实例sql:
SELECT * FROM `employee` LEFT JOIN `dept` ON `employee`.dept_id = `dept`.dept_id WHERE `dept`.dept_id IS NULL
UNION
SELECT * FROM `employee` RIGHT JOIN `dept` ON `employee`.dept_id = `dept`.dept_id WHERE `employee`.dept_id IS NULL;
+------+---------------+---------+---------+-----------+
| id   | employee_name | dept_id | dept_id | dept_name |
+------+---------------+---------+---------+-----------+
|    5 | liuqi         |     100 |    NULL | NULL      |
| NULL | NULL          |    NULL |       5 | 运营部    |
| NULL | NULL          |    NULL |       6 | 财务部    |
+------+---------------+---------+---------+-----------+

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mysql示例数据库 employee,这个大家也可以到github官网下载。 https://github.com/datacharmer/test_db test_db A sample database with an integrated test suite, used to test your applications and database servers This repository was migrated from Launchpad. See usage in the MySQL docs Where it comes from The original data was created by Fusheng Wang and Carlo Zaniolo at Siemens Corporate Research. The data is in XML format. http://timecenter.cs.aau.dk/software.htm Giuseppe Maxia made the relational schema and Patrick Crews exported the data in relational format. The database contains about 300,000 employee records with 2.8 million salary entries. The export data is 167 MB, which is not huge, but heavy enough to be non-trivial for testing. The data was generated, and as such there are inconsistencies and subtle problems. Rather than removing them, we decided to leave the contents untouched, and use these issues as data cleaning exercises. Prerequisites You need a MySQL database server (5.0+) and run the commands below through a user that has the following privileges: SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW Installation: Download the repository Change directory to the repository Then run mysql < employees.sql If you want to install with two large partitioned tables, run mysql < employees_partitioned.sql Testing the installation After installing, you can run one of the following mysql -t < test_employees_md5.sql # OR mysql -t < test_employees_sha.sql For example: mysql -t < test_employees_md5.sql +----------------------+ | INFO | +----------------------+ | TESTING INSTALLATION | +----------------------+ +--------------+------------------+----------------------------------+ | table_name | expected_records | expected_crc | +--------------+------------------+----------------------------------+ | employees | 300024 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值