Loading

多表查询


返回 我的技术栈(Technology Stack)



多表查询,也称为关联查询,指两个或更多个表一起完成查询操作。

前提条件:这些一起查询的表之间是有关系的(一对一、一对多),它们之间一定是有关联字段,这个关联字段可能建立了外键,也可能没有建立外键。比如:员工表和部门表,这两个表依靠“部门编号”进行关联。

一、 一个案例引发的多表连接

1. 案例说明

image
从多个表中获取数据:
image

#案例:查询员工的姓名及其部门名称
SELECT last_name, department_name FROM employees, departments;
查询结果:
+-----------+----------------------+
| last_name | department_name      |
+-----------+----------------------+
| King      | Administration       |
| King      | Marketing            |
| King      | Purchasing           |
| King      | Human Resources      |
| King      | Shipping             |
| King      | IT                   |
| King      | Public Relations     |
| King      | Sales                |
| King      | Executive            |
| King      | Finance              |
| King      | Accounting           |
| ....      | ........             |
| Gietz     | IT Support           |
| Gietz     | NOC                  |
| Gietz     | IT Helpdesk          |
| Gietz     | Government Sales     |
| Gietz     | Retail Sales         |
| Gietz     | Recruiting           |
| Gietz     | Payroll              |
+-----------+----------------------+
2889 rows in set (0.01 sec)

image
分析错误情况:

SELECT COUNT(employee_id) FROM employees;
#输出107行

SELECT COUNT(department_id)FROM departments;
#输出27行

SELECT 107*27 FROM dual;
#输出2889行

我们把上述多表查询中出现的问题称为:笛卡尔积的错误。

2. 笛卡尔积(或交叉连接)的理解

笛卡尔乘积是一个数学运算。假设我有两个集合 X 和 Y,那么 X 和 Y 的笛卡尔积就是 X 和 Y 的所有可能组合,也就是第一个对象来自于 X,第二个对象来自于 Y 的所有可能。组合的个数即为两个集合中元素个数的乘积数。
image
SQL92中,笛卡尔积也称为交叉连接,英文是 CROSS JOIN。在 SQL99 中也是使用 CROSS JOIN表示交叉连接。它的作用就是可以把任意表进行连接,即使这两张表不相关。在MySQL中如下情况会出现笛卡尔积:

#查询员工姓名和所在部门名称
SELECT last_name,department_name FROM employees,departments;
SELECT last_name,department_name FROM employees CROSS JOIN departments;
SELECT last_name,department_name FROM employees INNER JOIN departments;
SELECT last_name,department_name FROM employees JOIN departments;

3. 案例分析与问题解决

  • 笛卡尔积的错误会在下面条件下产生

    • 省略多个表的连接条件(或关联条件)
    • 连接条件(或关联条件)无效
    • 所有表中的所有行互相连接
  • 为了避免笛卡尔积, 可以在 WHERE 加入有效的连接条件。

  • 加入连接条件后,查询语法:

    SELECT	table1.column, table2.column
    FROM	table1, table2
    WHERE	table1.column1 = table2.column2;  #连接条件
    
    • 在 WHERE子句中写入连接条件。
  • 正确写法:

    #案例:查询员工的姓名及其部门名称
    SELECT last_name, department_name
    FROM employees, departments
    WHERE employees.department_id = departments.department_id;
    
  • 在表中有相同列时,在列名之前加上表名前缀。

二、多表查询分类

1. 等值连接 vs 非等值连接

  • 等值连接
    image
SELECT employees.employee_id, employees.last_name, 
       employees.department_id, departments.department_id,
       departments.location_id
FROM   employees, departments
WHERE  employees.department_id = departments.department_id;

image

表的别名

  • 使用别名可以简化查询。

  • 列名前使用表名前缀可以提高查询效率。

SELECT e.employee_id, e.last_name, e.department_id,
       d.department_id, d.location_id
FROM   employees e , departments d
WHERE  e.department_id = d.department_id;

需要注意的是,如果我们使用了表的别名,在查询字段中、过滤条件中就只能使用别名进行代替,不能使用原有的表名,否则就会报错。

阿里开发规范

强制】对于数据库中表记录的查询和变更,只要涉及多个表,都需要在列名前加表的别名(或 表名)进行限定。

说明:对多表进行查询记录、更新记录、删除记录时,如果对操作列没有限定表的别名(或表名),并且操作列在多个表中存在时,就会抛异常。

正例:select t1.name from table_first as t1 , table_second as t2 where t1.id=t2.id;

反例:在某业务中,由于多表关联查询语句没有加表的别名(或表名)的限制,正常运行两年后,最近在 某个表中增加一个同名字段,在预发布环境做数据库变更后,线上查询语句出现出 1052 异常:Column 'name' in field list is ambiguous。

  • 非等值连接
    image
SELECT e.last_name, e.salary, j.grade_level
FROM   employees e, job_grades j
WHERE  e.salary BETWEEN j.lowest_sal AND j.highest_sal;

image

2. 自连接 vs 非自连接

image
当table1和table2本质上是同一张表,只是用取别名的方式虚拟成两张表以代表不同的意义。然后两个表再进行内连接,外连接等查询。
例如:查询employees表,返回“Xxx works for Xxx”

SELECT CONCAT(worker.last_name ,' works for ' 
       , manager.last_name)
FROM   employees worker, employees manager
WHERE  worker.manager_id = manager.employee_id ;

image

3. 内连接 vs 外连接

除了查询满足条件的记录以外,外连接还可以查询某一方不满足条件的记录。

  • 内连接: 合并具有同一列的两个以上的表的行, 结果集中不包含一个表与另一个表不匹配的行

  • 外连接: 两个表在连接过程中除了返回满足连接条件的行以外还返回左(或右)表中不满足条件的行 ,这种连接称为左(或右) 外连接。没有匹配的行时, 结果表中相应的列为空(NULL)。

    • 如果是左外连接,则连接条件中左边的表也称为主表,右边的表称为从表

    • 如果是右外连接,则连接条件中右边的表也称为主表,左边的表称为从表

  • 内连接(INNER JOIN)

创建 customers 表:

CREATE TABLE customers (
	id INT PRIMARY KEY,
    customer_id INT,
    customer_name VARCHAR(50)
);

插入数据到 customers 表:

INSERT INTO customers (id, customer_id, customer_name)
VALUES
    (1,0001,'John'),
    (2,0010, 'Jane'),
    (3,0011, 'Smith'),
    (4,0100, 'Michael'),
    (5,0101, NULL),
    (6,NULL, 'JACK'),
    (7,0111, 'Sarah');

创建 orders 表:

CREATE TABLE orders (
	id INT PRIMARY KEY,
    order_id INT,
    customer_id INT,
    order_date DATE,
    amount DECIMAL(10, 2)
);

插入数据到 orders 表:

INSERT INTO orders (id, order_id, customer_id, order_date, amount)
VALUES
    (1, 1,0001, '2023-01-01', 100.00),
    (2, 1,0010, '2023-02-01', 150.00),
    (3, 2,1001, '2023-03-01', 200.00),
    (4, 3,0011, NULL, 50.00),
    (5, 4,NULL, '2023-04-01', NULL),
    (6, NULL,NULL, NULL, NULL);
mysql> select * from customers;
+----+-------------+---------------+
| id | customer_id | customer_name |
+----+-------------+---------------+
|  1 |           1 | John          |
|  2 |          10 | Jane          |
|  3 |          11 | Smith         |
|  4 |         100 | Michael       |
|  5 |         101 | NULL          |
|  6 |        NULL | JACK          |
|  7 |         111 | Sarah         |
+----+-------------+---------------+
7 rows in set (0.00 sec)

mysql> select * from orders;
+----+----------+-------------+------------+--------+
| id | order_id | customer_id | order_date | amount |
+----+----------+-------------+------------+--------+
|  1 |        1 |           1 | 2023-01-01 | 100.00 |
|  2 |        1 |          10 | 2023-02-01 | 150.00 |
|  3 |        2 |        1001 | 2023-03-01 | 200.00 |
|  4 |        3 |          11 | NULL       |  50.00 |
|  5 |        4 |        NULL | 2023-04-01 |   NULL |
|  6 |     NULL |        NULL | NULL       |   NULL |
+----+----------+-------------+------------+--------+
6 rows in set (0.00 sec)

三、9种join方式

image

INNER JOIN

mysql> select * from customers;
+----+-------------+---------------+
| id | customer_id | customer_name |
+----+-------------+---------------+
|  1 |           1 | John          |
|  2 |          10 | Jane          |
|  3 |          11 | Smith         |
|  4 |         100 | Michael       |
|  5 |         101 | NULL          |
|  6 |        NULL | JACK          |
|  7 |         111 | Sarah         |
+----+-------------+---------------+
7 rows in set (0.00 sec)

mysql> select * from orders;
+----+----------+-------------+------------+--------+
| id | order_id | customer_id | order_date | amount |
+----+----------+-------------+------------+--------+
|  1 |        1 |           1 | 2023-01-01 | 100.00 |
|  2 |        1 |          10 | 2023-02-01 | 150.00 |
|  3 |        2 |        1001 | 2023-03-01 | 200.00 |
|  4 |        3 |          11 | NULL       |  50.00 |
|  5 |        4 |        NULL | 2023-04-01 |   NULL |
|  6 |     NULL |        NULL | NULL       |   NULL |
+----+----------+-------------+------------+--------+
6 rows in set (0.00 sec)

mysql> SELECT  o.order_id, o.customer_id, o.order_date, o.amount, c.customer_name
    -> FROM orders o
    -> INNER JOIN customers c
    -> ON o.customer_id = c.customer_id;
+----------+-------------+------------+--------+---------------+
| order_id | customer_id | order_date | amount | customer_name |
+----------+-------------+------------+--------+---------------+
|        1 |           1 | 2023-01-01 | 100.00 | John          |
|        1 |          10 | 2023-02-01 | 150.00 | Jane          |
|        3 |          11 | NULL       |  50.00 | Smith         |
+----------+-------------+------------+--------+---------------+
3 rows in set (0.00 sec)

image

LEFT JOIN

左外连接,则连接条件中左边的表也称为主表,右边的表称为从表

mysql> SELECT * FROM orders;
+----+----------+-------------+------------+--------+
| id | order_id | customer_id | order_date | amount |
+----+----------+-------------+------------+--------+
|  1 |        1 |           1 | 2023-01-01 | 100.00 |
|  2 |        1 |          10 | 2023-02-01 | 150.00 |
|  3 |        2 |        1001 | 2023-03-01 | 200.00 |
|  4 |        3 |          11 | NULL       |  50.00 |
|  5 |        4 |        NULL | 2023-04-01 |   NULL |
|  6 |     NULL |        NULL | NULL       |   NULL |
+----+----------+-------------+------------+--------+
6 rows in set (0.00 sec)

mysql> SELECT * FROM customers;
+----+-------------+---------------+
| id | customer_id | customer_name |
+----+-------------+---------------+
|  1 |           1 | John          |
|  2 |          10 | Jane          |
|  3 |          11 | Smith         |
|  4 |         100 | Michael       |
|  5 |         101 | NULL          |
|  6 |        NULL | JACK          |
|  7 |         111 | Sarah         |
+----+-------------+---------------+
7 rows in set (0.00 sec)

mysql> SELECT  o.order_id, o.customer_id, c.customer_id, o.order_date, o.amount, c.customer_name
    -> FROM orders o
    -> LEFT JOIN customers c
    -> ON o.customer_id = c.customer_id;
+----------+-------------+-------------+------------+--------+---------------+
| order_id | customer_id | customer_id | order_date | amount | customer_name |
+----------+-------------+-------------+------------+--------+---------------+
|        1 |           1 |           1 | 2023-01-01 | 100.00 | John          |
|        1 |          10 |          10 | 2023-02-01 | 150.00 | Jane          |
|        2 |        1001 |        NULL | 2023-03-01 | 200.00 | NULL          |
|        3 |          11 |          11 | NULL       |  50.00 | Smith         |
|        4 |        NULL |        NULL | 2023-04-01 |   NULL | NULL          |
|     NULL |        NULL |        NULL | NULL       |   NULL | NULL          |
+----------+-------------+-------------+------------+--------+---------------+
6 rows in set (0.00 sec)

mysql> SELECT  o.order_id, o.customer_id, c.customer_id, o.order_date, o.amount, c.customer_name
    -> FROM customers c
    -> LEFT JOIN orders o
    -> ON o.customer_id = c.customer_id;
+----------+-------------+-------------+------------+--------+---------------+
| order_id | customer_id | customer_id | order_date | amount | customer_name |
+----------+-------------+-------------+------------+--------+---------------+
|        1 |           1 |           1 | 2023-01-01 | 100.00 | John          |
|        1 |          10 |          10 | 2023-02-01 | 150.00 | Jane          |
|        3 |          11 |          11 | NULL       |  50.00 | Smith         |
|     NULL |        NULL |         100 | NULL       |   NULL | Michael       |
|     NULL |        NULL |         101 | NULL       |   NULL | NULL          |
|     NULL |        NULL |        NULL | NULL       |   NULL | JACK          |
|     NULL |        NULL |         111 | NULL       |   NULL | Sarah         |
+----------+-------------+-------------+------------+--------+---------------+
7 rows in set (0.00 sec)

image

RIGHT JOIN

右外连接,则连接条件中右边的表也称为主表,左边的表称为从表

mysql> SELECT * FROM orders;
+----+----------+-------------+------------+--------+
| id | order_id | customer_id | order_date | amount |
+----+----------+-------------+------------+--------+
|  1 |        1 |           1 | 2023-01-01 | 100.00 |
|  2 |        1 |          10 | 2023-02-01 | 150.00 |
|  3 |        2 |        1001 | 2023-03-01 | 200.00 |
|  4 |        3 |          11 | NULL       |  50.00 |
|  5 |        4 |        NULL | 2023-04-01 |   NULL |
|  6 |     NULL |        NULL | NULL       |   NULL |
+----+----------+-------------+------------+--------+
6 rows in set (0.00 sec)

mysql> SELECT * FROM customers;
+----+-------------+---------------+
| id | customer_id | customer_name |
+----+-------------+---------------+
|  1 |           1 | John          |
|  2 |          10 | Jane          |
|  3 |          11 | Smith         |
|  4 |         100 | Michael       |
|  5 |         101 | NULL          |
|  6 |        NULL | JACK          |
|  7 |         111 | Sarah         |
+----+-------------+---------------+
7 rows in set (0.00 sec)

mysql> SELECT  o.order_id, o.customer_id, c.customer_id, o.order_date, o.amount, c.customer_name
    -> FROM orders o
    -> RIGHT JOIN customers c
    -> ON o.customer_id = c.customer_id
    -> WHERE c.customer_id IS NULL;
+----------+-------------+-------------+------------+--------+---------------+
| order_id | customer_id | customer_id | order_date | amount | customer_name |
+----------+-------------+-------------+------------+--------+---------------+
|     NULL |        NULL |        NULL | NULL       |   NULL | JACK          |
+----------+-------------+-------------+------------+--------+---------------+
1 row in set (0.00 sec)

mysql> SELECT  o.order_id, o.customer_id, c.customer_id, o.order_date, o.amount, c.customer_name
    -> FROM customers c
    -> RIGHT JOIN orders o
    -> ON o.customer_id = c.customer_id;
+----------+-------------+-------------+------------+--------+---------------+
| order_id | customer_id | customer_id | order_date | amount | customer_name |
+----------+-------------+-------------+------------+--------+---------------+
|        1 |           1 |           1 | 2023-01-01 | 100.00 | John          |
|        1 |          10 |          10 | 2023-02-01 | 150.00 | Jane          |
|        2 |        1001 |        NULL | 2023-03-01 | 200.00 | NULL          |
|        3 |          11 |          11 | NULL       |  50.00 | Smith         |
|        4 |        NULL |        NULL | 2023-04-01 |   NULL | NULL          |
|     NULL |        NULL |        NULL | NULL       |   NULL | NULL          |
+----------+-------------+-------------+------------+--------+---------------+
6 rows in set (0.00 sec)

image

LEFT JOIN WHERE IS NULL

mysql> SELECT * FROM orders;
+----+----------+-------------+------------+--------+
| id | order_id | customer_id | order_date | amount |
+----+----------+-------------+------------+--------+
|  1 |        1 |           1 | 2023-01-01 | 100.00 |
|  2 |        1 |          10 | 2023-02-01 | 150.00 |
|  3 |        2 |        1001 | 2023-03-01 | 200.00 |
|  4 |        3 |          11 | NULL       |  50.00 |
|  5 |        4 |        NULL | 2023-04-01 |   NULL |
|  6 |     NULL |        NULL | NULL       |   NULL |
+----+----------+-------------+------------+--------+
6 rows in set (0.00 sec)

mysql> SELECT * FROM customers;
+----+-------------+---------------+
| id | customer_id | customer_name |
+----+-------------+---------------+
|  1 |           1 | John          |
|  2 |          10 | Jane          |
|  3 |          11 | Smith         |
|  4 |         100 | Michael       |
|  5 |         101 | NULL          |
|  6 |        NULL | JACK          |
|  7 |         111 | Sarah         |
+----+-------------+---------------+
7 rows in set (0.00 sec)

mysql> SELECT  o.order_id, o.customer_id, c.customer_id, o.order_date, o.amount, c.customer_name
    -> FROM orders o
    -> LEFT JOIN customers c
    -> ON o.customer_id = c.customer_id;
+----------+-------------+-------------+------------+--------+---------------+
| order_id | customer_id | customer_id | order_date | amount | customer_name |
+----------+-------------+-------------+------------+--------+---------------+
|        1 |           1 |           1 | 2023-01-01 | 100.00 | John          |
|        1 |          10 |          10 | 2023-02-01 | 150.00 | Jane          |
|        2 |        1001 |        NULL | 2023-03-01 | 200.00 | NULL          |
|        3 |          11 |          11 | NULL       |  50.00 | Smith         |
|        4 |        NULL |        NULL | 2023-04-01 |   NULL | NULL          |
|     NULL |        NULL |        NULL | NULL       |   NULL | NULL          |
+----------+-------------+-------------+------------+--------+---------------+
6 rows in set (0.00 sec)

mysql> SELECT  o.order_id, o.customer_id, c.customer_id, o.order_date, o.amount, c.customer_name
    -> FROM orders o
    -> LEFT JOIN customers c
    -> ON o.customer_id = c.customer_id
    -> WHERE c.customer_id IS NULL;
+----------+-------------+-------------+------------+--------+---------------+
| order_id | customer_id | customer_id | order_date | amount | customer_name |
+----------+-------------+-------------+------------+--------+---------------+
|        2 |        1001 |        NULL | 2023-03-01 | 200.00 | NULL          |
|        4 |        NULL |        NULL | 2023-04-01 |   NULL | NULL          |
|     NULL |        NULL |        NULL | NULL       |   NULL | NULL          |
+----------+-------------+-------------+------------+--------+---------------+
3 rows in set (0.00 sec)

image

RIGHT JOIN WHERE IS NULL

mysql> SELECT * FROM orders;
+----+----------+-------------+------------+--------+
| id | order_id | customer_id | order_date | amount |
+----+----------+-------------+------------+--------+
|  1 |        1 |           1 | 2023-01-01 | 100.00 |
|  2 |        1 |          10 | 2023-02-01 | 150.00 |
|  3 |        2 |        1001 | 2023-03-01 | 200.00 |
|  4 |        3 |          11 | NULL       |  50.00 |
|  5 |        4 |        NULL | 2023-04-01 |   NULL |
|  6 |     NULL |        NULL | NULL       |   NULL |
+----+----------+-------------+------------+--------+
6 rows in set (0.00 sec)

mysql> SELECT * FROM customers;
+----+-------------+---------------+
| id | customer_id | customer_name |
+----+-------------+---------------+
|  1 |           1 | John          |
|  2 |          10 | Jane          |
|  3 |          11 | Smith         |
|  4 |         100 | Michael       |
|  5 |         101 | NULL          |
|  6 |        NULL | JACK          |
|  7 |         111 | Sarah         |
+----+-------------+---------------+
7 rows in set (0.00 sec)

mysql> SELECT  o.order_id, o.customer_id, c.customer_id, o.order_date, o.amount, c.customer_name
    -> FROM orders o
    -> RIGHT JOIN customers c
    -> ON o.customer_id = c.customer_id;
+----------+-------------+-------------+------------+--------+---------------+
| order_id | customer_id | customer_id | order_date | amount | customer_name |
+----------+-------------+-------------+------------+--------+---------------+
|        1 |           1 |           1 | 2023-01-01 | 100.00 | John          |
|        1 |          10 |          10 | 2023-02-01 | 150.00 | Jane          |
|        3 |          11 |          11 | NULL       |  50.00 | Smith         |
|     NULL |        NULL |         100 | NULL       |   NULL | Michael       |
|     NULL |        NULL |         101 | NULL       |   NULL | NULL          |
|     NULL |        NULL |        NULL | NULL       |   NULL | JACK          |
|     NULL |        NULL |         111 | NULL       |   NULL | Sarah         |
+----------+-------------+-------------+------------+--------+---------------+
7 rows in set (0.00 sec)

mysql> SELECT  o.order_id, o.customer_id, c.customer_id, o.order_date, o.amount, c.customer_name
    -> FROM orders o
    -> RIGHT JOIN customers c
    -> ON o.customer_id = c.customer_id
    -> WHERE o.customer_id IS NULL;
+----------+-------------+-------------+------------+--------+---------------+
| order_id | customer_id | customer_id | order_date | amount | customer_name |
+----------+-------------+-------------+------------+--------+---------------+
|     NULL |        NULL |         100 | NULL       |   NULL | Michael       |
|     NULL |        NULL |         101 | NULL       |   NULL | NULL          |
|     NULL |        NULL |        NULL | NULL       |   NULL | JACK          |
|     NULL |        NULL |         111 | NULL       |   NULL | Sarah         |
+----------+-------------+-------------+------------+--------+---------------+
4 rows in set (0.00 sec)

image

四、UNION的使用

合并查询结果
利用UNION关键字,可以给出多条SELECT语句,并将它们的结果组合成单个结果集。合并时,两个表对应的列数和数据类型必须相同,并且相互对应。各个SELECT语句之间使用UNION或UNION ALL关键字分隔。

语法格式:

SELECT column,... FROM table1
UNION [ALL]
SELECT column,... FROM table2

UNION操作符

image

UNION 操作符返回两个查询的结果集的并集,去除重复记录

UNION ALL操作符

image

UNION ALL操作符返回两个查询的结果集的并集。对于两个结果集的重复部分,不去重

注意:执行UNION ALL语句时所需要的资源比UNION语句少。如果明确知道合并数据后的结果数据不存在重复数据,或者不需要去除重复的数据,则尽量使用UNION ALL语句,以提高数据查询的效率。

举例:查询部门编号>90或邮箱包含a的员工信息

#方式1
SELECT * FROM employees WHERE email LIKE '%a%' OR department_id>90;
#方式2
SELECT * FROM employees  WHERE email LIKE '%a%'
UNION
SELECT * FROM employees  WHERE department_id>90;

举例:查询中国用户中男性的信息以及美国用户中年男性的用户信息

SELECT id,cname FROM t_chinamale WHERE csex='男'
UNION ALL
SELECT id,tname FROM t_usmale WHERE tGender='male';

五、扩展:SQL99语法新特性

1.自然连接

SQL99 在 SQL92 的基础上提供了一些特殊语法,比如 NATURAL JOIN 用来表示自然连接。我们可以把自然连接理解为 SQL92 中的等值连接。它会帮你自动查询两张连接表中所有相同的字段,然后进行等值连接

在SQL92标准中:

SELECT employee_id,last_name,department_name
FROM employees e JOIN departments d
ON e.`department_id` = d.`department_id`
AND e.`manager_id` = d.`manager_id`;

在 SQL99 中你可以写成:

SELECT employee_id,last_name,department_name
FROM employees e NATURAL JOIN departments d;

2.USING连接

当我们进行连接的时候,SQL99还支持使用 USING 指定数据表里的同名字段进行等值连接。但是只能配合JOIN一起使用。比如:

SELECT employee_id,last_name,department_name
FROM employees e JOIN departments d
USING (department_id);

你能看出与自然连接 NATURAL JOIN 不同的是,USING 指定了具体的相同的字段名称,你需要在 USING 的括号 () 中填入要指定的同名字段。同时使用 JOIN...USING 可以简化 JOIN ON 的等值连接。它与下面的 SQL 查询结果是相同的:

SELECT employee_id,last_name,department_name
FROM employees e ,departments d
WHERE e.department_id = d.department_id;

多表连接查询

在增加一个顾客地址表

CREATE TABLE city (
	  id INT PRIMARY KEY,
    customer_name VARCHAR(50),
		location_city VARCHAR(50)
);

INSERT INTO city (id, customer_name, location_city)
VALUES
    (1,'John','四川'),
    (2, 'Jane','北京'),
    (3, 'Smith','上海'),
    (4, 'JACK','杭州'),
    (5, 'Sarah','深圳');

查询三张表

mysql> SELECT * FROM  orders;
+----+----------+-------------+------------+--------+
| id | order_id | customer_id | order_date | amount |
+----+----------+-------------+------------+--------+
|  1 |        1 |           1 | 2023-01-01 | 100.00 |
|  2 |        1 |          10 | 2023-02-01 | 150.00 |
|  3 |        2 |        1001 | 2023-03-01 | 200.00 |
|  4 |        3 |          11 | NULL       |  50.00 |
|  5 |        4 |        NULL | 2023-04-01 |   NULL |
|  6 |     NULL |        NULL | NULL       |   NULL |
+----+----------+-------------+------------+--------+
6 rows in set (0.00 sec)

mysql> SELECT * FROM  customers;
+----+-------------+---------------+
| id | customer_id | customer_name |
+----+-------------+---------------+
|  1 |           1 | John          |
|  2 |          10 | Jane          |
|  3 |          11 | Smith         |
|  4 |         100 | Michael       |
|  5 |         101 | NULL          |
|  6 |        NULL | JACK          |
|  7 |         111 | Sarah         |
+----+-------------+---------------+
7 rows in set (0.00 sec)

mysql> SELECT * FROM  city;
+----+---------------+---------------+
| id | customer_name | location_city |
+----+---------------+---------------+
|  1 | John          | 四川          |
|  2 | Jane          | 北京          |
|  3 | Smith         | 上海          |
|  4 | JACK          | 杭州          |
|  5 | Sarah         | 深圳          |
+----+---------------+---------------+
5 rows in set (0.00 sec)

多表连接查询

mysql> SELECT * FROM  city;
+----+---------------+---------------+
| id | customer_name | location_city |
+----+---------------+---------------+
|  1 | John          | 四川          |
|  2 | Jane          | 北京          |
|  3 | Smith         | 上海          |
|  4 | JACK          | 杭州          |
|  5 | Sarah         | 深圳          |
+----+---------------+---------------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM  customers;
+----+-------------+---------------+
| id | customer_id | customer_name |
+----+-------------+---------------+
|  1 |           1 | John          |
|  2 |          10 | Jane          |
|  3 |          11 | Smith         |
|  4 |         100 | Michael       |
|  5 |         101 | NULL          |
|  6 |        NULL | JACK          |
|  7 |         111 | Sarah         |
+----+-------------+---------------+
7 rows in set (0.00 sec)

mysql> SELECT * FROM  orders;
+----+----------+-------------+------------+--------+
| id | order_id | customer_id | order_date | amount |
+----+----------+-------------+------------+--------+
|  1 |        1 |           1 | 2023-01-01 | 100.00 |
|  2 |        1 |          10 | 2023-02-01 | 150.00 |
|  3 |        2 |        1001 | 2023-03-01 | 200.00 |
|  4 |        3 |          11 | NULL       |  50.00 |
|  5 |        4 |        NULL | 2023-04-01 |   NULL |
|  6 |     NULL |        NULL | NULL       |   NULL |
+----+----------+-------------+------------+--------+
6 rows in set (0.00 sec)

mysql> SELECT   o.order_id, o.customer_id, o.order_date, o.amount, c.customer_id, c.customer_name, d.customer_name, d.location_city
    -> FROM orders o
    -> LEFT JOIN customers c
    -> ON o.customer_id = c.customer_id
    -> LEFT JOIN city d
    -> ON c.customer_name = d.customer_name;
+----------+-------------+------------+--------+-------------+---------------+---------------+---------------+
| order_id | customer_id | order_date | amount | customer_id | customer_name | customer_name | location_city |
+----------+-------------+------------+--------+-------------+---------------+---------------+---------------+
|        1 |           1 | 2023-01-01 | 100.00 |           1 | John          | John          | 四川          |
|        1 |          10 | 2023-02-01 | 150.00 |          10 | Jane          | Jane          | 北京          |
|        2 |        1001 | 2023-03-01 | 200.00 |        NULL | NULL          | NULL          | NULL          |
|        3 |          11 | NULL       |  50.00 |          11 | Smith         | Smith         | 上海          |
|        4 |        NULL | 2023-04-01 |   NULL |        NULL | NULL          | NULL          | NULL          |
|     NULL |        NULL | NULL       |   NULL |        NULL | NULL          | NULL          | NULL          |
+----------+-------------+------------+--------+-------------+---------------+---------------+---------------+
6 rows in set (0.00 sec)

image

image

附录:常用的 SQL 标准有哪些

在正式开始讲连接表的种类时,我们首先需要知道 SQL 存在不同版本的标准规范,因为不同规范下的表连接操作是有区别的。

SQL 有两个主要的标准,分别是 SQL92SQL99。92 和 99 代表了标准提出的时间,SQL92 就是 92 年提出的标准规范。当然除了 SQL92 和 SQL99 以外,还存在 SQL-86、SQL-89、SQL:2003、SQL:2008、SQL:2011 和 SQL:2016 等其他的标准。

这么多标准,到底该学习哪个呢?实际上最重要的 SQL 标准就是 SQL92 和 SQL99。一般来说 SQL92 的形式更简单,但是写的 SQL 语句会比较长,可读性较差。而 SQL99 相比于 SQL92 来说,语法更加复杂,但可读性更强。我们从这两个标准发布的页数也能看出,SQL92 的标准有 500 页,而 SQL99 标准超过了 1000 页。实际上从 SQL99 之后,很少有人能掌握所有内容,因为确实太多了。就好比我们使用 Windows、Linux 和 Office 的时候,很少有人能掌握全部内容一样。我们只需要掌握一些核心的功能,满足日常工作的需求即可。

SQL92 和 SQL99 是经典的 SQL 标准,也分别叫做 SQL-2 和 SQL-3 标准。也正是在这两个标准发布之后,SQL 影响力越来越大,甚至超越了数据库领域。现如今 SQL 已经不仅仅是数据库领域的主流语言,还是信息领域中信息处理的主流语言。在图形检索、图像检索以及语音检索中都能看到 SQL 语言的使用。

参考:
https://www.bilibili.com/video/BV1iq4y1u7vj/?spm_id_from=333.337.search-card.all.click&vd_source=661c82170c96dfe1717bbda84ba2a36f

posted @ 2023-09-02 12:28  言非  阅读(62)  评论(0编辑  收藏  举报