题目
表: 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 表的外键
buyer_id 和 seller_id 是 Users 表的外键
表: Items
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| item_id | int |
| item_brand | varchar |
+---------------+---------+
item_id 是该表的主键
写一个 SQL 查询确定每一个用户按日期顺序卖出的第二件商品的品牌是否是他们最喜爱的品牌。如果一个用户卖出少于两件商品,查询的结果是 no 。
题目保证没有一个用户在一天中卖出超过一件商品
下面是查询结果格式的例子:
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 |
+---------+------------+
Result table:
+-----------+--------------------+
| seller_id | 2nd_item_fav_brand |
+-----------+--------------------+
| 1 | no |
| 2 | yes |
| 3 | yes |
| 4 | no |
+-----------+--------------------+
id 为 1 的用户的查询结果是 no,因为他什么也没有卖出
id为 2 和 3 的用户的查询结果是 yes,因为他们卖出的第二件商品的品牌是他们自己最喜爱的品牌
id为 4 的用户的查询结果是 no,因为他卖出的第二件商品的品牌不是他最喜爱的品牌
更新数据
DROP TABLE Orders4;
CREATE TABLE Orders4 (order_id INT, order_date DATE, item_id INT, buyer_id INT, seller_id INT)
INSERT INTO Orders4 (order_id, order_date, item_id, buyer_id, seller_id) VALUES ('1', '2019-08-01', '4', '1', '2');
INSERT INTO Orders4 (order_id, order_date, item_id, buyer_id, seller_id) VALUES ('2', '2018-08-02', '2', '1', '3');
INSERT INTO Orders4 (order_id, order_date, item_id, buyer_id, seller_id) VALUES ('3', '2019-08-03', '3', '2', '3');
INSERT INTO Orders4 (order_id, order_date, item_id, buyer_id, seller_id) VALUES ('4', '2019-08-04', '1', '4', '2');
INSERT INTO Orders4 (order_id, order_date, item_id, buyer_id, seller_id) VALUES ('5', '2019-08-04', '1', '3', '4');
INSERT INTO Orders4 (order_id, order_date, item_id, buyer_id, seller_id) VALUES ('6', '2019-08-05', '2', '2', '4');
解答
还是利用分组取rank的方法给定每个seller_id的按时间排序的rank
SELECT *, @rnk:= IF(@pre_sell_id = O.seller_id, @rnk+1, 1) AS rnk, @pre_sell_id:=O.seller_id
FROM Orders4 AS O, (SELECT @rnk:=0, @pre_sell_id:= NULL) AS init
ORDER BY O.`seller_id`, O.`order_date` ASC;
取出rank为2的代表卖出的第二件商品
SELECT tmp.seller_id, tmp.item_id
FROM (SELECT *, @rnk:= IF(@pre_sell_id = O.seller_id, @rnk+1, 1) AS rnk, @pre_sell_id:=O.seller_id
FROM Orders4 AS O, (SELECT @rnk:=0, @pre_sell_id:= NULL) AS init
ORDER BY O.`seller_id`, O.`order_date` ASC) AS tmp
WHERE tmp.rnk = 2;
查询每个用户最喜欢的商品的id
SELECT U.`user_id`, I.`item_id`
FROM Users1 AS U
JOIN Items AS I
ON U.`favorite_brand` = I.`item_brand`;
以上两个临时表按id连接 如果为空则取no 在判断item_id是否相等 相等yes 不等no
SELECT A.user_id AS seller_id, IF(B.seller_id IS NULL, 'no', IF(A.item_id = B.item_id, 'yes', 'no')) AS '2nd_item_fav_brand'
FROM (SELECT U.`user_id`, I.`item_id`
FROM Users1 AS U
JOIN Items AS I
ON U.`favorite_brand` = I.`item_brand`) AS A
LEFT JOIN (SELECT tmp.seller_id, tmp.item_id
FROM (SELECT *, @rnk:= IF(@pre_sell_id = O.seller_id, @rnk+1, 1) AS rnk, @pre_sell_id:=O.seller_id
FROM Orders4 AS O, (SELECT @rnk:=0, @pre_sell_id:= NULL) AS init
ORDER BY O.`seller_id`, O.`order_date` ASC) AS tmp
WHERE tmp.rnk = 2) AS B
ON A.user_id = B.seller_id
ORDER BY A.user_id;
利用子查询也可以
SELECT
Users.user_id AS 'seller_id',
if(O3.item_id is not null AND Users.favorite_brand=Items.item_brand,'yes','no') AS '2nd_item_fav_brand'
FROM
Users
LEFT JOIN
(
SELECT seller_id,(
SELECT
item_id
FROM
Orders O2
WHERE
O2.seller_id=O1.seller_id
ORDER BY order_date LIMIT 1,1
) AS 'item_id'
FROM
Orders O1
GROUP BY seller_id
) O3
ON Users.user_id=O3.seller_id
LEFT JOIN
Items
ON O3.item_id=Items.item_id