美文网首页
Leetcode1398.购买了产品A和产品B却没有购买产品C的

Leetcode1398.购买了产品A和产品B却没有购买产品C的

作者: kaka22 | 来源:发表于2020-07-24 09:52 被阅读0次

题目
Table: Customers

+---------------------+---------+
| Column Name         | Type    |
+---------------------+---------+
| customer_id         | int     |
| customer_name       | varchar |
+---------------------+---------+

customer_id is the primary key for this table.
customer_name is the name of the customer.

Table: Orders

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| order_id      | int     |
| customer_id   | int     |
| product_name  | varchar |
+---------------+---------+

order_id is the primary key for this table.
customer_id is the id of the customer who bought the product "product_name".

Write an SQL query to report the customer_id and customer_name of customers who bought products "A", "B" but did not buy the product "C" since we want to recommend them buy this product.

Return the result table ordered by customer_id.

The query result format is in the following example.

Customers table:

+-------------+---------------+
| customer_id | customer_name |
+-------------+---------------+
| 1           | Daniel        |
| 2           | Diana         |
| 3           | Elizabeth     |
| 4           | Jhon          |
+-------------+---------------+

Orders table:

+------------+--------------+---------------+
| order_id   | customer_id  | product_name  |
+------------+--------------+---------------+
| 10         |     1        |     A         |
| 20         |     1        |     B         |
| 30         |     1        |     D         |
| 40         |     1        |     C         |
| 50         |     2        |     A         |
| 60         |     3        |     A         |
| 70         |     3        |     B         |
| 80         |     3        |     D         |
| 90         |     4        |     C         |
+------------+--------------+---------------+

Result table:

+-------------+---------------+
| customer_id | customer_name |
+-------------+---------------+
| 3           | Elizabeth     |
+-------------+---------------+

Only the customer_id with id 3 bought the product A and B but not the product C.

解答
先选出同时购买了A和B的顾客id

select distinct O1.customer_id
from Orders as O1
join Orders as O2
on O1.customer_id = O2.customer_id
where O1.product_name = 'A' and O2.product_name = 'B' 

再选出购买了C的顾客id

select distinct O1.customer_id
from Orders as O1
where O1.product_name = 'C'

对Customers 选出 id在{同时购买了A和B的顾客id} 但不在{购买了C的顾客id}的即可

select C.customer_id, C.customer_name
from Customers as C
where C.customer_id in (select distinct O1.customer_id
from Orders as O1
join Orders as O2
on O1.customer_id = O2.customer_id
where O1.product_name = 'A' and O2.product_name = 'B' ) and 
C.customer_id not in (select distinct O1.customer_id
from Orders as O1
where O1.product_name = 'C')

相关文章

  • Leetcode1398.购买了产品A和产品B却没有购买产品C的

    题目Table: Customers customer_id is the primary key for thi...

  • 管理信息系统

    20180415 京东 B-B-C 购买产品自营,提供产品,赚取差价 淘宝 C-b-C 连接了个体户和顾客,提供平...

  • 互联网行业的产品方向

    根据产品属性和目标,互联网产品分为C端产品、B端产品、数据和策略产品、商品变现产品和AI产品。 A. 什么是C端产...

  • B端产品和C端产品的区别总结

    一、定义 To B产品:B指的是Business,顾名思义To B产品就是面向企业用户的产品。 To C产品:C指...

  • 基于用户购买

    基于用户推荐产品 a,b用户之间相似度计算公式=(a,b两用户共同购买东西件数)/(a购买产品件数*b购买产品件数...

  • 2021-01-17

    B端产品和C端产品:找相通 对,你没看错,是“找相通”,不是“找相同”。 关于B端产品和C端产品的差异及共性,是个...

  • B端与C端产品建设流程的区别

    B端产品和C端产品建设流程对比 产品面对的用户不同 C端产品的用户是独立的个人,而不是一个组织或机构。 B端产品用...

  • To B产品和To C产品的不同

    参考资料:ToB互联网产品有哪些坑1.B端产品对商业思维的要求比较高,体验也需要关注,但是最重要的是怎么帮助用户更...

  • 解密互联网行业5大产品方向

    根据产品的属性和目标,业界习惯将互联网产品分为C端产品、B端产品、数据与策略产品、 商业变现产品和AI...

  • 关于“产品”的小想法一二

    如果产品分为2B和2C的话,那2B的产品经理需要做到的是从功能产品经理到业务产品经理的晋升;而对于2C的产品经理就...

网友评论

      本文标题:Leetcode1398.购买了产品A和产品B却没有购买产品C的

      本文链接:https://www.haomeiwen.com/subject/uradlktx.html