# Write your MySQL query statement below
1159. 市场分析 II
表: Users
+----------------+---------+ | Column Name | Type | +----------------+---------+ | user_id | int | | join_date | date | | favorite_brand | varchar | +----------------+---------+ user_id 是该表的主键(具有唯一值的列) 表中包含一位在线购物网站用户的个人信息,用户可以在该网站出售和购买商品。
表: Orders
+---------------+---------+ | Column Name | Type | +---------------+---------+ | order_id | int | | order_date | date | | item_id | int | | buyer_id | int | | seller_id | int | +---------------+---------+ order_id 是该表的主键(具有唯一值的列) item_id 是 Items 表的外键(reference 列) buyer_id 和 seller_id 是 Users 表的外键
表: Items
+---------------+---------+ | Column Name | Type | +---------------+---------+ | item_id | int | | item_brand | varchar | +---------------+---------+ item_id 是该表的主键(具有唯一值的列)
编写解决方案找出每一个用户按日期顺序卖出的第二件商品的品牌是否是他们最喜爱的品牌。如果一个用户卖出少于两件商品,查询的结果是 no
。题目保证没有一个用户在一天中卖出超过一件商品。
以 任意顺序 返回结果表
返回结果格式的例子如下所示:
示例 1:
输入: Users table: +---------+------------+----------------+ | user_id | join_date | favorite_brand | +---------+------------+----------------+ | 1 | 2019-01-01 | Lenovo | | 2 | 2019-02-09 | Samsung | | 3 | 2019-01-19 | LG | | 4 | 2019-05-21 | HP | +---------+------------+----------------+ Orders table: +----------+------------+---------+----------+-----------+ | order_id | order_date | item_id | buyer_id | seller_id | +----------+------------+---------+----------+-----------+ | 1 | 2019-08-01 | 4 | 1 | 2 | | 2 | 2019-08-02 | 2 | 1 | 3 | | 3 | 2019-08-03 | 3 | 2 | 3 | | 4 | 2019-08-04 | 1 | 4 | 2 | | 5 | 2019-08-04 | 1 | 3 | 4 | | 6 | 2019-08-05 | 2 | 2 | 4 | +----------+------------+---------+----------+-----------+ Items table: +---------+------------+ | item_id | item_brand | +---------+------------+ | 1 | Samsung | | 2 | Lenovo | | 3 | LG | | 4 | HP | +---------+------------+ 输出: +-----------+--------------------+ | seller_id | 2nd_item_fav_brand | +-----------+--------------------+ | 1 | no | | 2 | yes | | 3 | yes | | 4 | no | +-----------+--------------------+ 解释: id 为 1 的用户的查询结果是 no,因为他什么也没有卖出 id为 2 和 3 的用户的查询结果是 yes,因为他们卖出的第二件商品的品牌是他们自己最喜爱的品牌 id为 4 的用户的查询结果是 no,因为他卖出的第二件商品的品牌不是他最喜爱的品牌
原站题解
mysql 解法, 执行用时: 918 ms, 内存消耗: 0 B, 提交时间: 2023-10-15 23:12:16
select user_id as seller_id, if (r2.item_brand is null || r2.item_brand != favorite_brand, "no", "yes") as 2nd_item_fav_brand from users left join ( select r1.seller_id, items.item_brand from ( select @rk := if (@seller = a.seller_id, @rk + 1, 1) as rank1, @seller := a.seller_id as seller_id, a.item_id from ( select seller_id, item_id from orders order by seller_id, order_date ) a, (select @seller := -1, @rk := 0) b) r1 join items on r1.item_id = items.item_id where r1.rank1 = 2 ) r2 on user_id = r2.seller_id;