题解 | #返回电子邮件

返回购买 prod_id 为 BR01 的产品的所有顾客的电子邮件(一)

https://www.nowcoder.com/practice/962b16554fbf4b99a87f4d68020c5bfb

直接用三个子查询

SELECT
    cust_email
FROM
    Customers
WHERE
    cust_id IN
    (
        SELECT
            cust_id
        FROM
            Orders
        WHERE
            order_num IN (
                SELECT
                    order_num
                FROM
                    OrderItems
                WHERE
                    prod_id = "BR01"
            )
    );

直接使用WHER子句

SELECT
    cust_email
FROM
    Customers,
    OrderItems,
    Orders
WHERE
    Customers.cust_id = Orders.cust_id
    AND Orders.order_num = OrderItems.order_num
    AND OrderItems.prod_id = "BR01";

使用左连接

首先将Customers和Orders以cust_id为依据连接在一起,

之后以order_num为依据将连接后的表和OrderItems连接在一起。

SELECT
    cust_email
FROM
    Customers c
    LEFT JOIN Orders o ON c.cust_id = o.cust_id
    LEFT JOIN OrderItems oi ON oi.order_num = o.order_num
WHERE
    oi.prod_id = "BR01";

全部评论

相关推荐

10-12 19:08
666 C++
花开蝶自来_:技能:听动物叫,让雪豹闭嘴
点赞 评论 收藏
分享
10-10 17:54
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务