mysql - 如何从表中查询数据,其中分钟、天、月和年存储在单独的列中

标签 mysql sql

我有一个遗留的 mysql 数据库,其中它的日期信息与其他数据列一起存储在单独的列中,如所附屏幕截图所示。

enter image description here

我想使用日期时间值或最好是时间戳过滤特定范围内的数据。

最好的方法是什么?

请找到示例数据的插入查询

create table MOCK_DATA (
    ServiceName VARCHAR(5),
    StatusType VARCHAR(8),
    Status VARCHAR(7),
    Year VARCHAR(4),
    Month VARCHAR(10),
    Day VARCHAR(10),
    Minute VARCHAR(10),
    Count VARCHAR(10)
);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test2', 'statusT1', 'status1', 2020, 7, 11, 29, 183);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test4', 'statusT4', 'status4', 2019, 4, 16, 15, 120);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test4', 'statusT1', 'status1', 2019, 8, 10, 37, 66);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test3', 'statusT4', 'status1', 2022, 6, 22, 32, 78);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test2', 'statusT2', 'status1', 2019, 9, 29, 36, 132);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test1', 'statusT4', 'status3', 2022, 11, 10, 29, 185);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test2', 'statusT4', 'status2', 2019, 8, 10, 56, 175);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test3', 'statusT1', 'status2', 2020, 6, 9, 24, 124);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test4', 'statusT4', 'status1', 2021, 3, 2, 40, 185);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test3', 'statusT4', 'status1', 2019, 5, 2, 12, 198);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test3', 'statusT1', 'status3', 2020, 12, 30, 33, 186);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test4', 'statusT4', 'status1', 2022, 10, 11, 26, 142);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test1', 'statusT3', 'status2', 2022, 10, 9, 18, 42);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test2', 'statusT1', 'status2', 2020, 1, 29, 33, 56);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test1', 'statusT3', 'status2', 2022, 4, 6, 33, 193);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test1', 'statusT1', 'status1', 2020, 2, 13, 20, 43);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test1', 'statusT3', 'status3', 2022, 10, 30, 36, 47);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test4', 'statusT4', 'status4', 2021, 2, 28, 56, 97);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test4', 'statusT3', 'status2', 2021, 2, 28, 8, 194);
insert into MOCK_DATA (ServiceName, StatusType, Status, Year, Month, Day, Minute, Count) values ('test2', 'statusT3', 'status2', 2021, 5, 27, 36, 199);

最佳答案

MySQL 5.7 及更高版本支持虚拟列和虚拟列上的索引:

ALTER TABLE MyTable 
  ADD COLUMN ServiceDate DATE AS (STR_TO_DATE(CONCAT(`Year`, '-', `Month`, '-', `Day`), '%Y-%m-%d')), 
  ADD KEY (ServiceDate);

您可以使用 EXPLAIN 来验证它是否使用该索引:

EXPLAIN SELECT * FROM mytable WHERE ServiceDate = CURDATE();
+----+-------------+---------+------------+------+---------------+-------------+---------+-------+------+----------+-------+
| id | select_type | table   | partitions | type | possible_keys | key         | key_len | ref   | rows | filtered | Extra |
+----+-------------+---------+------------+------+---------------+-------------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | mytable | NULL       | ref  | ServiceDate   | ServiceDate | 4       | const |    1 |   100.00 | NULL  |
+----+-------------+---------+------------+------+---------------+-------------+---------+-------+------+----------+-------+

如果需要时间戳,可以将虚拟列设置为 DATETIME,并使基于表达式的虚拟列也使用小时、分钟和秒列。

关于mysql - 如何从表中查询数据,其中分钟、天、月和年存储在单独的列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72550303/

相关文章:

MySQL插入查询时间较长

php - 在 session php 数组中显示下一条记录

sql - 我们可以在子查询中使用 LIKE 而不是 IN

sql - Firebird Cursors - 你为什么要使用一个

mysql - 表 'tbl_name' 被指定了两次,既作为 'UPDATE' 的目标又作为单独的数据源

MySQL 查找速度比任何电脑都慢的笔记本电脑

sql - GROUP BY 语句中逗号分隔的整数有什么作用?

mysql - 自动将 0 或空字符串转换为 NULL

PHP 超链接/从 SQL DB 中获取单个记录

mysql - 不同表中多个值的总和