gpt4 book ai didi

mysql - 具有相同列的 MySQL 中的 DateDiff

转载 作者:可可西里 更新时间:2023-11-01 08:08:56 27 4
gpt4 key购买 nike

我有一个如下所示的“订单”表:

+---------------+--------------+------------+
| customer_name | order_number | date |
+---------------+--------------+------------+
| jack | 1 | 2018-01-01 |
| jack | 2 | 2018-01-06 |
| jack | 3 | 2018-01-19 |
| jack | 4 | 2018-01-06 |
| jack | 5 | 2018-02-27 |
| jack | 6 | 2018-02-02 |
+---------------+--------------+------------+

现在,我想要一个表格,以天为单位给出连续日期的差异。像这样:

+------------+------------+------+
| date | next_date | diff |
+------------+------------+------+
| 2018-01-01 | 2018-01-06 | 5 |
| 2018-01-06 | 2018-01-06 | 0 |
| 2018-01-06 | 2018-01-19 | 13 |
| 2018-01-19 | 2018-02-02 | 14 |
| 2018-02-02 | 2018-02-27 | 25 |
+------------+------------+------+

我使用的查询是这样的:

SELECT orders.date, MIN(table1.date) FROM orders
LEFT JOIN orders table1
on orders.customer_name = table1.customer_name
AND table1.date >= orders.date
AND table1.order_number != orders.order_number
WHERE orders.customer_name = 'jack'
GROUP BY orders.order_number, orders.date
ORDER BY orders.date;

这是输出:

+------------+------------+
| date | next_date |
+------------+------------+
| 2018-01-01 | 2018-01-06 |
| 2018-01-06 | 2018-01-06 |
| 2018-01-06 | 2018-01-06 |
| 2018-01-19 | 2018-02-02 |
| 2018-02-02 | 2018-02-27 |
| 2018-02-27 | NULL |
+------------+------------+

如您所见,存在一些问题。

  1. 有两行的datenext_date都是2018-01-06
  2. 没有 next_date 为 2018-01-19 的行`
  3. 最后一行的 next_date 的值为 NULL>
  4. 如何获得日期的天数差异?

我知道这是因为我已经按 order_number 和 >= 分组了但我不知道还有什么办法可以解决这个问题。我觉得有一个明显的简单解决方案正在逃避我。有帮助吗?

SQL Fiddle

如果 SQL Fiddle 不工作:

CREATE TABLE orders
(`customer_name` varchar(4), `order_number` int, `date` varchar(10))
;

INSERT INTO orders
(`customer_name`, `order_number`, `date`)
VALUES
('jack', 1, '2018-01-01'),
('jack', 2, '2018-01-06'),
('jack', 3, '2018-01-19'),
('jack', 4, '2018-01-06'),
('jack', 5, '2018-02-27'),
('jack', 6, '2018-02-02')
;

最佳答案

如果您对依赖于 MySQL 8.x 或更高版本的非 ROW_NUMBER() 解决方案感兴趣,请查看下一个说明。

解释:

1) 首先,我们从 orders 表中选择所有日期,通过升序对它们进行排序,并为每个日期分配一个虚拟的自动增量 ID。我们将得到这样的东西:

SELECT (@row_number := @row_number + 1) AS orderNum, date 
FROM ORDERS, (SELECT @row_number:=0) AS t
ORDER BY date;

Output:
1 2018-01-01
2 2018-01-06
3 2018-01-06
4 2018-01-19
5 2018-02-02
6 2018-02-27

2) 我们创建了一个与前一个类似的查询,但这次我们丢弃了第一行,如下所示:

SELECT (@row_number2 := @row_number2 + 1) AS orderNum, date
FROM ORDERS, (SELECT @row_number2 := 0) AS t
ORDER BY date
LIMIT 999999999999
OFFSET 1;

Output:
1 2018-01-06
2 2018-01-06
3 2018-01-19
4 2018-02-02
5 2018-02-27

这里唯一的问题是,我们必须将 LIMIT 数字硬编码为足够大的数字,这样我们才能确保我们会选择除第一行以外的所有行。

3) 此时,您应该考虑通过虚拟生成的 ID 连接之前的两个结果。那么,让我们看看最终的查询:

SELECT
startDate.date AS date,
nextDate.date AS next_date,
DATEDIFF(nextDate.date, startDate.date) AS diff
FROM
(SELECT (@row_number := @row_number + 1) AS orderNum, date
FROM ORDERS, (SELECT @row_number:=0) AS t
ORDER BY date) AS startDate
INNER JOIN
(SELECT (@row_number2 := @row_number2 + 1) AS orderNum, date
FROM ORDERS, (SELECT @row_number2 := 0) AS t
ORDER BY date
LIMIT 999999999999
OFFSET 1) AS nextDate ON nextDate.orderNum = startDate.orderNum;

Output:
2018-01-01 2018-01-06 5
2018-01-06 2018-01-06 0
2018-01-06 2018-01-19 13
2018-01-19 2018-02-02 14
2018-02-02 2018-02-27 25

您可以在此处查看工作示例:http://sqlfiddle.com/#!9/1572ea/27

关于mysql - 具有相同列的 MySQL 中的 DateDiff,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52862381/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com