gpt4 book ai didi

mysql - 仅适用于某些情况的查询

转载 作者:可可西里 更新时间:2023-11-01 07:45:38 25 4
gpt4 key购买 nike

我有一个类似于以下结构的表:

City        start_date             end_date
Paris 1995-01-01 00:00:00 1997-10-01 23:59:59
Paris 1997-10-02 00:00:00 0001-01-01 00:00:00
Paris 2013-01-25 00:00:00 0001-01-01 00:00:00
Paris 2015-04-25 00:00:00 0001-01-01 00:00:00
Berlin 2014-11-01 00:00:00 0001-01-01 00:00:00
Berlin 2014-06-01 00:00:00 0001-01-01 00:00:00
Berlin 2015-09-11 00:00:00 0001-01-01 00:00:00
Berlin 2015-10-01 00:00:00 0001-01-01 00:00:00
Milan 2001-01-01 00:00:00 0001-01-01 00:00:00
Milan 2005-10-02 00:00:00 2006-10-02 23:59:59
Milan 2006-10-03 00:00:00 2015-04-24 23:59:59
Milan 2015-04-25 00:00:00 0001-01-01 00:00:00

数据包含基于城市的开始日期和结束日期的历史 View 。一个城市的最新记录应该是开始日期最长的记录,结束日期为'0001-01-01 00:00:00',表示还没有结束日期。

我需要清理这些数据并确保每个城市的历史记录都有结束日期比下一条记录的开始日期早一秒,只有在 end_date 设置为“0001-”的情况下01-01 00:00:00'。因此,在 end_date 有实际日期的情况下,该日期将被忽略。此外,城市的 start_date 最近的记录不需要修改 end_date。

结果表应该是这样的:

City        start_date             end_date
Paris 1995-01-01 00:00:00 1997-10-01 23:59:59
Paris 1997-10-02 00:00:00 2013-01-24 23:59:59
Paris 2013-01-25 00:00:00 2015-04-24 23:59:59
Paris 2015-04-25 00:00:00 0001-01-01 00:00:00
Berlin 2014-11-01 00:00:00 2014-05-31 23:59:59
Berlin 2014-06-01 00:00:00 2015-09-10 23:59:59
Berlin 2015-09-11 00:00:00 2015-09-30 23:59:59
Berlin 2015-10-01 00:00:00 0001-01-01 23:59:59
Milan 2001-01-01 00:00:00 2005-10-01 23:59:59
Milan 2005-10-02 00:00:00 2006-10-02 23:59:59
Milan 2006-10-03 00:00:00 2015-04-24 23:59:59
Milan 2015-04-25 00:00:00 0001-01-01 00:00:00

我已经尝试了以下用户在 this question 中建议的脚本.

update test join
(select t.*,
(select min(start_date)
from test t2
where t2.city = t.city and
t2.start_date > t.start_date
order by t2.start_date
limit 1
) as next_start_date
from test t
) tt
on tt.city = test.city and tt.start_date = test.start_date
set test.end_date = date_sub(tt.next_start_date, interval 1 second)
where test.end_date = '0001-01-01' and
next_start_date is not null;

不幸的是,从柏林记录开始,一些结束日期不是预期的(例如 ID 号 5 和 6)。然而,其他人正在按预期出现。如下所示:

enter image description here

以下是能够复制的创建和插入语句:

CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`city` varchar(50) DEFAULT NULL,
`start_date` datetime DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

INSERT INTO test (city, start_date, end_date) VALUES ('Paris','1995-01-01 00:00:00','1997-10-01 23:59:59');
INSERT INTO test (city, start_date, end_date) VALUES ('Paris','1997-10-02 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Paris','2013-01-25 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Paris','2015-04-25 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Berlin','2014-11-01 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Berlin','2014-06-01 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Berlin','2015-09-11 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Berlin','2015-10-01 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Milan','2001-01-01 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Milan','2005-10-02 00:00:00','2006-10-02 23:59:59');
INSERT INTO test (city, start_date, end_date) VALUES ('Milan','2006-10-03 00:00:00','2015-04-24 23:59:59');
INSERT INTO test (city, start_date, end_date) VALUES ('Milan','2015-04-25 00:00:00','0001-01-01 00:00:00');

最佳答案

-- query wanted
UPDATE test t1 INNER JOIN
(SELECT *, @id := @id + 1 AS new_id
FROM test CROSS JOIN (SELECT @id := 0) param
ORDER BY city, start_date) t2
ON t1.city = t2.city AND t1.start_date = t2.start_date
INNER JOIN
(SELECT *, @id2 := @id2 + 1 AS new_id
FROM test CROSS JOIN (SELECT @id2 := 0) param
ORDER BY city, start_date) t3
ON t2.new_id + 1 = t3.new_id AND t2.city = t3.city
SET t1.end_date = DATE_SUB(t3.start_date, INTERVAL 1 SECOND)
WHERE t1.end_date = '0001-01-01 00:00:00';

下面是完整的demo。

SQL:

-- data
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`city` varchar(50) DEFAULT NULL,
`start_date` datetime DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

INSERT INTO test (city, start_date, end_date) VALUES ('Paris','1995-01-01 00:00:00','1997-10-01 23:59:59');
INSERT INTO test (city, start_date, end_date) VALUES ('Paris','1997-10-02 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Paris','2013-01-25 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Paris','2015-04-25 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Berlin','2014-11-01 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Berlin','2014-06-01 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Berlin','2015-09-11 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Berlin','2015-10-01 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Milan','2001-01-01 00:00:00','0001-01-01 00:00:00');
INSERT INTO test (city, start_date, end_date) VALUES ('Milan','2005-10-02 00:00:00','2006-10-02 23:59:59');
INSERT INTO test (city, start_date, end_date) VALUES ('Milan','2006-10-03 00:00:00','2015-04-24 23:59:59');
INSERT INTO test (city, start_date, end_date) VALUES ('Milan','2015-04-25 00:00:00','0001-01-01 00:00:00');
select * from test;

-- query wanted
UPDATE test t1 INNER JOIN
(SELECT *, @id := @id + 1 AS new_id
FROM test CROSS JOIN (SELECT @id := 0) param
ORDER BY city, start_date) t2
ON t1.city = t2.city AND t1.start_date = t2.start_date
INNER JOIN
(SELECT *, @id2 := @id2 + 1 AS new_id
FROM test CROSS JOIN (SELECT @id2 := 0) param
ORDER BY city, start_date) t3
ON t2.new_id + 1 = t3.new_id AND t2.city = t3.city
SET t1.end_date = DATE_SUB(t3.start_date, INTERVAL 1 SECOND)
WHERE t1.end_date = '0001-01-01 00:00:00';

select * from test;

输出:

mysql> -- query wanted
mysql> UPDATE test t1 INNER JOIN
-> (SELECT *, @id := @id + 1 AS new_id
-> FROM test CROSS JOIN (SELECT @id := 0) param
-> ORDER BY city, start_date) t2
-> ON t1.city = t2.city AND t1.start_date = t2.start_date
-> INNER JOIN
-> (SELECT *, @id2 := @id2 + 1 AS new_id
-> FROM test CROSS JOIN (SELECT @id2 := 0) param
-> ORDER BY city, start_date) t3
-> ON t2.new_id + 1 = t3.new_id AND t2.city = t3.city
-> SET t1.end_date = DATE_SUB(t3.start_date, INTERVAL 1 SECOND)
-> WHERE t1.end_date = '0001-01-01 00:00:00';
rom tesQuery OK, 6 rows affected (0.00 sec)
Rows matched: 6 Changed: 6 Warnings: 0

mysql> select * from test;
+----+--------+---------------------+---------------------+
| id | city | start_date | end_date |
+----+--------+---------------------+---------------------+
| 13 | Paris | 1995-01-01 00:00:00 | 1997-10-01 23:59:59 |
| 14 | Paris | 1997-10-02 00:00:00 | 2013-01-24 23:59:59 |
| 15 | Paris | 2013-01-25 00:00:00 | 2015-04-24 23:59:59 |
| 16 | Paris | 2015-04-25 00:00:00 | 0001-01-01 00:00:00 |
| 17 | Berlin | 2014-11-01 00:00:00 | 2014-05-31 23:59:59 |
| 18 | Berlin | 2014-06-01 00:00:00 | 2015-09-10 23:59:59 |
| 19 | Berlin | 2015-09-11 00:00:00 | 2015-09-30 23:59:59 |
| 20 | Berlin | 2015-10-01 00:00:00 | 0001-01-01 00:00:00 |
| 21 | Milan | 2001-01-01 00:00:00 | 2005-10-01 23:59:59 |
| 22 | Milan | 2005-10-02 00:00:00 | 2006-10-02 23:59:59 |
| 23 | Milan | 2006-10-03 00:00:00 | 2015-04-24 23:59:59 |
| 24 | Milan | 2015-04-25 00:00:00 | 0001-01-01 00:00:00 |
+----+--------+---------------------+---------------------+
12 rows in set (0.00 sec)

关于mysql - 仅适用于某些情况的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36125799/

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