Projection 子句中的子查询

子查询可发生在另一个 SELECT 语句的 Projection 子句中。下列查询显示如何在 Projection 子句中使用子查询来返回 customer 表中每个顾客的总装运费用(来自 orders 表)。还可以将此查询编写为两个表之间的连接。

图: 查询

SELECT customer.customer_num,
          (SELECT SUM(ship_charge) 
          FROM orders
          WHERE customer.customer_num = orders.customer_num) 
          AS total_ship_chg
          FROM customer;

图: 查询结果

customer_num total_ship_chg
            
            101         $15.30
            102
            103
            104         $38.00
            105
            ⋮
            123          $8.50
            124         $12.00
            125
            126         $13.00
            127         $18.00
            128