如何使用 MySQL NOT EXISTS 运算符
How to use the MySQL NOT EXISTS operator
在 table calendar_recovery_interval_hour_2020
上,我将日子划分为每个一小时的时间段,例如2020-04-14
+---------------------+---------------------+-----+
| start_date | end_date | sID |
+---------------------+---------------------+-----+
| 2020-04-14 00:00:00 | 2020-04-14 00:59:00 | 1 |
| 2020-04-14 01:00:00 | 2020-04-14 01:59:00 | 2 |
| 2020-04-14 02:00:00 | 2020-04-14 02:59:00 | 3 |
| 2020-04-14 03:00:00 | 2020-04-14 03:59:00 | 4 |
| 2020-04-14 04:00:00 | 2020-04-14 04:59:00 | 5 |
| 2020-04-14 05:00:00 | 2020-04-14 05:59:00 | 6 |
| 2020-04-14 06:00:00 | 2020-04-14 06:59:00 | 7 |
| 2020-04-14 07:00:00 | 2020-04-14 07:59:00 | 8 |
| 2020-04-14 08:00:00 | 2020-04-14 08:59:00 | 9 |
| 2020-04-14 09:00:00 | 2020-04-14 09:59:00 | 10 |
| 2020-04-14 10:00:00 | 2020-04-14 10:59:00 | 11 |
| 2020-04-14 11:00:00 | 2020-04-14 11:59:00 | 12 |
| 2020-04-14 12:00:00 | 2020-04-14 12:59:00 | 13 |
| 2020-04-14 13:00:00 | 2020-04-14 13:59:00 | 14 |
| 2020-04-14 14:00:00 | 2020-04-14 14:59:00 | 15 |
| 2020-04-14 15:00:00 | 2020-04-14 15:59:00 | 16 |
| 2020-04-14 16:00:00 | 2020-04-14 16:59:00 | 17 |
| 2020-04-14 17:00:00 | 2020-04-14 17:59:00 | 18 |
| 2020-04-14 18:00:00 | 2020-04-14 18:59:00 | 19 |
| 2020-04-14 19:00:00 | 2020-04-14 19:59:00 | 20 |
| 2020-04-14 20:00:00 | 2020-04-14 20:59:00 | 21 |
| 2020-04-14 21:00:00 | 2020-04-14 21:59:00 | 22 |
| 2020-04-14 22:00:00 | 2020-04-14 22:59:00 | 23 |
| 2020-04-14 23:00:00 | 2020-04-14 23:59:00 | 24 |
+---------------------+---------------------+-----+
24 rows in set
现在我需要在一秒钟内找到 table 相对于 table calendar_recovery_interval_hour_2020
丢失的时间段
这个tutorial
我在下面试过的代码没有成功
TRUNCATE TABLE `calendar_recovery_table_time_slot_2020`;
INSERT IGNORE INTO `calendar_recovery_table_time_slot_2020` SELECT
start_date,
end_date,
NULL
FROM
`calendar_recovery_interval_hour_2020` t
WHERE
NOT EXISTS (
SELECT
1
FROM
`stable_2020` m
WHERE
m.`sdatetime` BETWEEN t.start_date
AND t.end_date
AND SUBSTRING_INDEX(m.`sdatetime`, ' ', 1) = '2020-04-14'
ORDER BY
m.`sdatetime` DESC
)
AND SUBSTRING_INDEX(start_date, ' ', 1) = '2020-04-14';
我的 MySQL 版本是 5.5.62-log
列的名称是 sDatetime,类型是 Datetime。
有什么建议吗?
提前感谢您的帮助
更新
CREATE TABLE `calendar_recovery_table_time_slot_2020` (
`start_date` datetime DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
`sID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`sID`),
KEY `start_date` (`start_date`),
KEY `end_date` (`end_date`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `calendar_recovery_interval_hour_2020` (
`start_date` datetime DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
`sID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`sID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT;
CREATE TABLE `stable_2020` (
`STUX` int(11) DEFAULT NULL,
`sdatetime` datetime DEFAULT NULL,
`sID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`sID`),
UNIQUE KEY `ukey` (`STUX`,`sdatetime`) USING BTREE,
KEY `sdatetime` (`sdatetime`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
更新 2
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 00:00:00', '2020-4-14 00:59:00', 1);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 01:00:00', '2020-4-14 01:59:00', 2);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 02:00:00', '2020-4-14 02:59:00', 3);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 03:00:00', '2020-4-14 03:59:00', 4);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 04:00:00', '2020-4-14 04:59:00', 5);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 05:00:00', '2020-4-14 05:59:00', 6);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 06:00:00', '2020-4-14 06:59:00', 7);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 07:00:00', '2020-4-14 07:59:00', 8);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 08:00:00', '2020-4-14 08:59:00', 9);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 09:00:00', '2020-4-14 09:59:00', 10);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 10:00:00', '2020-4-14 10:59:00', 11);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 11:00:00', '2020-4-14 11:59:00', 12);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 12:00:00', '2020-4-14 12:59:00', 13);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 13:00:00', '2020-4-14 13:59:00', 14);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 14:00:00', '2020-4-14 14:59:00', 15);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 15:00:00', '2020-4-14 15:59:00', 16);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 16:00:00', '2020-4-14 16:59:00', 17);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 17:00:00', '2020-4-14 17:59:00', 18);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 18:00:00', '2020-4-14 18:59:00', 19);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 19:00:00', '2020-4-14 19:59:00', 20);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 20:00:00', '2020-4-14 20:59:00', 21);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 21:00:00', '2020-4-14 21:59:00', 22);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 22:00:00', '2020-4-14 22:59:00', 23);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 23:00:00', '2020-4-14 23:59:00', 24);
您可以像这样在 SQL 查询中添加 NOT BETWEEN 运算符:
SELECT
1
FROM
`stable_2020` m
WHERE
m.`sdatetime` NOT BETWEEN t.start_date
AND t.end_date
AND SUBSTRING_INDEX(m.`sdatetime`, ' ', 1) = '2020-04-14'
ORDER BY
m.`sdatetime` DESC
Here 是另一种解决方案。
这样您就永远不需要在查询中使用 NOT EXISTS 运算符。试试这个,让我知道,谢谢。
在 table calendar_recovery_interval_hour_2020
上,我将日子划分为每个一小时的时间段,例如2020-04-14
+---------------------+---------------------+-----+
| start_date | end_date | sID |
+---------------------+---------------------+-----+
| 2020-04-14 00:00:00 | 2020-04-14 00:59:00 | 1 |
| 2020-04-14 01:00:00 | 2020-04-14 01:59:00 | 2 |
| 2020-04-14 02:00:00 | 2020-04-14 02:59:00 | 3 |
| 2020-04-14 03:00:00 | 2020-04-14 03:59:00 | 4 |
| 2020-04-14 04:00:00 | 2020-04-14 04:59:00 | 5 |
| 2020-04-14 05:00:00 | 2020-04-14 05:59:00 | 6 |
| 2020-04-14 06:00:00 | 2020-04-14 06:59:00 | 7 |
| 2020-04-14 07:00:00 | 2020-04-14 07:59:00 | 8 |
| 2020-04-14 08:00:00 | 2020-04-14 08:59:00 | 9 |
| 2020-04-14 09:00:00 | 2020-04-14 09:59:00 | 10 |
| 2020-04-14 10:00:00 | 2020-04-14 10:59:00 | 11 |
| 2020-04-14 11:00:00 | 2020-04-14 11:59:00 | 12 |
| 2020-04-14 12:00:00 | 2020-04-14 12:59:00 | 13 |
| 2020-04-14 13:00:00 | 2020-04-14 13:59:00 | 14 |
| 2020-04-14 14:00:00 | 2020-04-14 14:59:00 | 15 |
| 2020-04-14 15:00:00 | 2020-04-14 15:59:00 | 16 |
| 2020-04-14 16:00:00 | 2020-04-14 16:59:00 | 17 |
| 2020-04-14 17:00:00 | 2020-04-14 17:59:00 | 18 |
| 2020-04-14 18:00:00 | 2020-04-14 18:59:00 | 19 |
| 2020-04-14 19:00:00 | 2020-04-14 19:59:00 | 20 |
| 2020-04-14 20:00:00 | 2020-04-14 20:59:00 | 21 |
| 2020-04-14 21:00:00 | 2020-04-14 21:59:00 | 22 |
| 2020-04-14 22:00:00 | 2020-04-14 22:59:00 | 23 |
| 2020-04-14 23:00:00 | 2020-04-14 23:59:00 | 24 |
+---------------------+---------------------+-----+
24 rows in set
现在我需要在一秒钟内找到 table 相对于 table calendar_recovery_interval_hour_2020
这个tutorial
我在下面试过的代码没有成功
TRUNCATE TABLE `calendar_recovery_table_time_slot_2020`;
INSERT IGNORE INTO `calendar_recovery_table_time_slot_2020` SELECT
start_date,
end_date,
NULL
FROM
`calendar_recovery_interval_hour_2020` t
WHERE
NOT EXISTS (
SELECT
1
FROM
`stable_2020` m
WHERE
m.`sdatetime` BETWEEN t.start_date
AND t.end_date
AND SUBSTRING_INDEX(m.`sdatetime`, ' ', 1) = '2020-04-14'
ORDER BY
m.`sdatetime` DESC
)
AND SUBSTRING_INDEX(start_date, ' ', 1) = '2020-04-14';
我的 MySQL 版本是 5.5.62-log
列的名称是 sDatetime,类型是 Datetime。
有什么建议吗?
提前感谢您的帮助
更新
CREATE TABLE `calendar_recovery_table_time_slot_2020` (
`start_date` datetime DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
`sID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`sID`),
KEY `start_date` (`start_date`),
KEY `end_date` (`end_date`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `calendar_recovery_interval_hour_2020` (
`start_date` datetime DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
`sID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`sID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT;
CREATE TABLE `stable_2020` (
`STUX` int(11) DEFAULT NULL,
`sdatetime` datetime DEFAULT NULL,
`sID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`sID`),
UNIQUE KEY `ukey` (`STUX`,`sdatetime`) USING BTREE,
KEY `sdatetime` (`sdatetime`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
更新 2
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 00:00:00', '2020-4-14 00:59:00', 1);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 01:00:00', '2020-4-14 01:59:00', 2);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 02:00:00', '2020-4-14 02:59:00', 3);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 03:00:00', '2020-4-14 03:59:00', 4);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 04:00:00', '2020-4-14 04:59:00', 5);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 05:00:00', '2020-4-14 05:59:00', 6);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 06:00:00', '2020-4-14 06:59:00', 7);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 07:00:00', '2020-4-14 07:59:00', 8);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 08:00:00', '2020-4-14 08:59:00', 9);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 09:00:00', '2020-4-14 09:59:00', 10);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 10:00:00', '2020-4-14 10:59:00', 11);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 11:00:00', '2020-4-14 11:59:00', 12);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 12:00:00', '2020-4-14 12:59:00', 13);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 13:00:00', '2020-4-14 13:59:00', 14);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 14:00:00', '2020-4-14 14:59:00', 15);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 15:00:00', '2020-4-14 15:59:00', 16);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 16:00:00', '2020-4-14 16:59:00', 17);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 17:00:00', '2020-4-14 17:59:00', 18);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 18:00:00', '2020-4-14 18:59:00', 19);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 19:00:00', '2020-4-14 19:59:00', 20);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 20:00:00', '2020-4-14 20:59:00', 21);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 21:00:00', '2020-4-14 21:59:00', 22);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 22:00:00', '2020-4-14 22:59:00', 23);
INSERT INTO calendar_recovery_interval_hour_2020 (`start_date`, `end_date`, `sID`) VALUES ('2020-4-14 23:00:00', '2020-4-14 23:59:00', 24);
您可以像这样在 SQL 查询中添加 NOT BETWEEN 运算符:
SELECT
1
FROM
`stable_2020` m
WHERE
m.`sdatetime` NOT BETWEEN t.start_date
AND t.end_date
AND SUBSTRING_INDEX(m.`sdatetime`, ' ', 1) = '2020-04-14'
ORDER BY
m.`sdatetime` DESC
Here 是另一种解决方案。
这样您就永远不需要在查询中使用 NOT EXISTS 运算符。试试这个,让我知道,谢谢。