看了SQL必知必会学了点之前不注意的东西

子查询:#

子查询常用于 WHERE 子 句的 IN 操作符中,以及用来填充计算列。

  1. 放在 where 里面

    sql
    1
    2
    3
    4
    5
    SELECT cust_id 
    FROM Orders
    WHERE order_num IN (SELECT order_num
    FROM OrderItems
    WHERE prod_id = 'RGAN01');
  2. 放在 select from 之间

    sql
    1
    2
    3
    4
    5
    6
    7
    SELECT cust_name, 
    cust_state,
    (SELECT COUNT(*)
    FROM Orders
    WHERE Orders.cust_id = Customers.cust_id) AS orders
    FROM Customers
    ORDER BY cust_name;

例题:

使用子查询,返回购买价格为 10 美元或以上产品的顾客列表。你需 要使用 OrderItems 表查找匹配的订单号(order_num),然后使用 Order 表检索这些匹配订单的顾客 ID(cust_id)

sql
1
2
3
4
5
6
7
SELECT DISTINCT cust_id
FROM Orders
WHERE order_num IN (
SELECT order_num
FROM OrderItems
WHERE purchase_price >= 10
);

你想知道订购 BR01 产品的日期。编写 SQL 语句,使用子查询来确定哪些订单(在 OrderItems 中)购买了 prod_id 为 BR01 的产品,然 后从 Orders 表中返回每个产品对应的顾客 ID(cust_id)和订单日期(order_date)。按订购日期对结果进行排序。

sql
1
2
3
4
5
6
7
SELECT cust_id , order_date
FROM Orders
WHERE order_num IN (
SELECT order_num
FROM OrderItems
WHERE prod_id = 'BR01'
)

现在我们让它更具挑战性。在上一个挑战题,返回购买 prod_id 为 BR01 的产品的所有顾客的电子邮件(Customers 表中的 cust_email)。 提示:这涉及 SELECT 语句,最内层的从 OrderItems 表返回 order_num, 中间的从 Customers 表返回 cust_id。

sql
1
2
3
4
5
6
7
8
9
10
11
SELECT cust_email
FROM Customers
WHERE cust_id IN (
SELECT cust_id
FROM Orders
WHERE order_num IN (
SELECT order_num
FROM OrderItemsm
WHERE prod_id = 'BR01'
)
)

Group by#

用于分组,处理组内数据;一般与聚合函数进行使用

where 不能与聚合函数一起使用 例如:where sum(xxxx)

having 总是与聚合函数一起使用,用来筛选出满足条件的组

image-20240911143855406

join#

性能低下

内连接

sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Customers 表:
+--------+----------+
| cust_id| cust_name|
+--------+----------+
| 1 | Alice |
| 2 | Bob |
| 3 | Carol |
| 4 | Dave |
+--------+----------+

Orders 表:
+--------+----------+----------+
| order_id| cust_id | order_date|
+--------+----------+----------+
| 101| 1 | 2023-07-01|
| 102| 2 | 2023-07-02|
| 103| 3 | 2023-07-03|
+--------+----------+----------+

-- 合并后:
INNER JOIN 查询:
+--------+----------+--------+----------+
| cust_id| cust_name| order_id| order_date|
+--------+----------+--------+----------+
| 1 | Alice | 101 | 2023-07-01|
| 2 | Bob | 102 | 2023-07-02|
| 3 | Carol | 103 | 2023-07-03|
+--------+----------+--------+----------+

外连接

sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Customers 表:
+--------+----------+
| cust_id| cust_name|
+--------+----------+
| 1 | Alice |
| 2 | Bob |
| 3 | Carol |
| 4 | Dave |
+--------+----------+

Orders 表:
+--------+----------+----------+
| order_id| cust_id | order_date|
+--------+----------+----------+
| 101| 1 | 2023-07-01|
| 102| 2 | 2023-07-02|
| 103| 3 | 2023-07-03|
+--------+----------+----------+


LEFT JOIN 查询结果:
+--------+----------+--------+----------+
| cust_id| cust_name| order_id| order_date|
+--------+----------+--------+----------+
| 1 | Alice | 101 | 2023-07-01|
| 2 | Bob | 102 | 2023-07-02|
| 3 | Carol | 103 | 2023-07-03|
| 4 | Dave | NULL | NULL |
+--------+----------+--------+----------+


RIGHT JOIN 查询结果:
+--------+----------+--------+----------+
| cust_id| cust_name| order_id| order_date|
+--------+----------+--------+----------+
| 1 | Alice | 101 | 2023-07-01|
| 2 | Bob | 102 | 2023-07-02|
| 3 | Carol | 103 | 2023-07-03|
| NULL | NULL | 101 | 2023-07-01|
| NULL | NULL | 102 | 2023-07-02|
| NULL | NULL | 103 | 2023-07-03|
+--------+----------+--------+----------+

内连接和外连接的区别:

内连接 就是取交集,返回的结果只有两个圆相交的部分

左外连接 就是取左边的圆,以及右边的圆相等的部分

右外连接 就是取右边的圆,以及左边的圆相等的部分

Union#

就是把查询到的数据当作一行,加在之前的一行上

sql
1
2
3
4
5
SELECT column1, column2, ...
FROM table1
UNION
SELECT column1, column2, ...
FROM table2;
sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
table1:
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+

table2:
+----+------+
| id | name |
+----+------+
| 2 | D |
| 3 | E |
| 4 | F |
+----+------+

SELECT * FROM table1
UNION
SELECT * FROM table2;

Result:
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
| 2 | D |
| 3 | E |
| 4 | F |
+----+------+