订单表order_table全部记录如下,查询连续3天下单的customer_name,比如zhangsan在12.1、12.2号和12.3号连续3天下单过,以下不符合预期的是()?
+----+----------+--------+---------+---------------+------------+
| id | order_id | sku_id | buy_num | customer_name | buy_date |
+----+----------+--------+---------+---------------+------------+
| 1 | 560001 | 1001 | 2 | zhangsan | 2021-12-01 |
| 2 | 560001 | 1002 | 1 | lisi | 2021-12-01 |
| 3 | 560002 | 1001 | 1 | zhangsan | 2021-12-02 |
| 4 | 560003 | 1003 | 2 | zhangsan | 2021-12-03 |
| 5 | 560004 | 1002 | 3 | lisi | 2021-12-02 |
+----+----------+--------+---------+---------------+------------+
5 rows in set (0.00 sec)
SELECT customer_name
FROM (
SELECT
customer_name,
buy_date,
LEAD(buy_date,1) OVER(partition by customer_name order by buy_date) as next1_buy_date,
LEAD(buy_date,2) OVER(partition by customer_name order by buy_date) as next2_buy_date
FROM
order_table
)t1
WHERE
TIMESTAMPDIFF(DAY,buy_date,next1_buy_date) = 1
and TIMESTAMPDIFF(DAY,buy_date,next2_buy_date) = 2 ;
SELECT customer_name
FROM (
SELECT
customer_name,
buy_date,
LAG(buy_date,1) OVER(partition by customer_name order by buy_date) as next1_buy_date,
LEAD(buy_date,1) OVER(partition by customer_name order by buy_date) as next2_buy_date
FROM
order_table
)t1
WHERE
TIMESTAMPDIFF(DAY,buy_date,next1_buy_date) = -1
and TIMESTAMPDIFF(DAY,buy_date,next2_buy_date) = -1 ;
SELECT customer_name
FROM (
SELECT
customer_name,
buy_date,
LAG(buy_date,1) OVER(partition by customer_name order by buy_date) as next1_buy_date,
LAG(buy_date,2) OVER(partition by customer_name order by buy_date) as next2_buy_date
FROM
order_table
)t1
WHERE
TIMESTAMPDIFF(DAY,buy_date,next1_buy_date) = -1
and TIMESTAMPDIFF(DAY,buy_date,next2_buy_date) = -2 ;
SELECT customer_name
FROM (
SELECT
customer_name,
buy_date,
LEAD(buy_date,1) OVER(partition by customer_name order by buy_date) as next1_buy_date,
LAG(buy_date,1) OVER(partition by customer_name order by buy_date) as next2_buy_date
FROM
order_table
)t1
WHERE
TIMESTAMPDIFF(DAY,buy_date,next1_buy_date) = 1
and TIMESTAMPDIFF(DAY,buy_date,next2_buy_date) = -1 ;