• 2084. 为订单类型为 0 的客户删除类型为 1 的订单


    SQL架构

    活动表: Orders

    +-------------+------+
    | Column Name | Type |
    +-------------+------+
    | order_id    | int  | 
    | customer_id | int  |
    | order_type  | int  | 
    +-------------+------+
    order_id是此表的主键列。
    此表的每一行都表示订单的ID、订购该订单的客户的ID以及订单类型。
    订单可以是类型0或类型1。
    

    编写SQL查询以根据以下条件报告所有订单:

    • 如果客户至少有一个类型为0的订单,则不要报告该客户的任何类型为1的订单。
    • 否则,报告客户的所有订单。

    按任意顺序返回结果表。

    查询结果格式如下例所示。

    示例 1:

    输入: 
    Orders table:
    +----------+-------------+------------+
    | order_id | customer_id | order_type |
    +----------+-------------+------------+
    | 1        | 1           | 0          |
    | 2        | 1           | 0          |
    | 11       | 2           | 0          |
    | 12       | 2           | 1          |
    | 21       | 3           | 1          |
    | 22       | 3           | 0          |
    | 31       | 4           | 1          |
    | 32       | 4           | 1          |
    +----------+-------------+------------+
    输出: 
    +----------+-------------+------------+
    | order_id | customer_id | order_type |
    +----------+-------------+------------+
    | 31       | 4           | 1          |
    | 32       | 4           | 1          |
    | 1        | 1           | 0          |
    | 2        | 1           | 0          |
    | 11       | 2           | 0          |
    | 22       | 3           | 0          |
    +----------+-------------+------------+
    解释: 
    客户1有两个类型为0的订单。我们两个都返回。
    客户2的订单类型为0,订单类型为1。我们只返回类型为0的订单。
    客户3的订单类型为0,订单类型为1。我们只返回类型为0的订单。
    客户4有两个类型1的订单。我们两个都返回。
    1. # Write your MySQL query statement below
    2. with t1 as (select
    3. customer_id #至少有一个类型为0的订单的客户
    4. from
    5. Orders
    6. group by
    7. customer_id
    8. having
    9. sum(if(order_type=0,1,0)) > 0
    10. ),
    11. t2 as (
    12. select
    13. o.order_id,o.customer_id,o.order_type,t1.customer_id customer_id2
    14. from
    15. Orders o left join t1
    16. using(customer_id)
    17. )
    18. select
    19. order_id,customer_id,order_type
    20. from
    21. t2
    22. where customer_id2 is null
    23. union all
    24. select
    25. order_id,customer_id,order_type
    26. from
    27. t2
    28. where customer_id2 is not null and order_type = 0
    1. # Write your MySQL query statement below
    2. with t1 as (select
    3. customer_id #至少有一个类型为0的订单的客户
    4. from
    5. Orders
    6. group by
    7. customer_id
    8. having
    9. sum(if(order_type=0,1,0)) > 0
    10. ),
    11. t2 as (
    12. select
    13. o.order_id,o.customer_id,o.order_type,t1.customer_id customer_id2
    14. from
    15. Orders o left join t1
    16. using(customer_id)
    17. )
    18. select
    19. order_id,customer_id,order_type #没有类型为0的订单的用户的所有内容
    20. from
    21. t2
    22. where customer_id2 is null
    23. union all
    24. select
    25. order_id,customer_id,order_type #至少有一个类型为0的订单,不要报告该客户的任何类型为1的订单 的所有内容
    26. from
    27. t2
    28. where customer_id2 is not null and order_type = 0

  • 相关阅读:
    千兆以太网(一)——RGMII与GMII接口
    SpringSecurity系列一:01 SpringSecurity 的自动配置原理
    搞懂Python正则表达式,这一篇就够了
    AndroidT(13) -- logger_write 库实现解析(四)
    JavaScript数组的扁平化:将 2D JavaScript 二维数组转换为 1D 一维数组(多种方法)
    年薪90万男子嫌无聊起诉公司?用任务软件飞项充实工作吧!
    【Android笔记18】Android中的Intent对象介绍及常见属性的使用
    【爬虫系列】用Pyqt5写一个爬虫小助手
    Linux系统调用六、stat函数与 struct stat 文件信息结构体深度刨析
    二次元商业计划书PPT模版
  • 原文地址:https://blog.csdn.net/m0_69157845/article/details/125621861