Quantcast
Channel: Oracle, MySQL, Sybase, Informix and other databases
Viewing all articles
Browse latest Browse all 1350

SQL NOT BETWEEN syntax in query on MySQL

$
0
0

Hi gurus,

I have problem with MySQL database

On the table calendar_recovery_interval_hour_2020 I divided the days into time slots of one hour each, e.g. 2020-04-14

+---------------------+---------------------+-----+| start_date          | end_date            | sID |+---------------------+---------------------+-----+|2020-04-1400:00:00|2020-04-1400:59:00|1||2020-04-1401:00:00|2020-04-1401:59:00|2||2020-04-1402:00:00|2020-04-1402:59:00|3||2020-04-1403:00:00|2020-04-1403:59:00|4||2020-04-1404:00:00|2020-04-1404:59:00|5||2020-04-1405:00:00|2020-04-1405:59:00|6||2020-04-1406:00:00|2020-04-1406:59:00|7||2020-04-1407:00:00|2020-04-1407:59:00|8||2020-04-1408:00:00|2020-04-1408:59:00|9||2020-04-1409:00:00|2020-04-1409:59:00|10||2020-04-1410:00:00|2020-04-1410:59:00|11||2020-04-1411:00:00|2020-04-1411:59:00|12||2020-04-1412:00:00|2020-04-1412:59:00|13||2020-04-1413:00:00|2020-04-1413:59:00|14||2020-04-1414:00:00|2020-04-1414:59:00|15||2020-04-1415:00:00|2020-04-1415:59:00|16||2020-04-1416:00:00|2020-04-1416:59:00|17||2020-04-1417:00:00|2020-04-1417:59:00|18||2020-04-1418:00:00|2020-04-1418:59:00|19||2020-04-1419:00:00|2020-04-1419:59:00|20||2020-04-1420:00:00|2020-04-1420:59:00|21||2020-04-1421:00:00|2020-04-1421:59:00|22||2020-04-1422:00:00|2020-04-1422:59:00|23||2020-04-1423:00:00|2020-04-1423:59:00|24|+---------------------+---------------------+-----+24rowsinset

Now I need to find in a second table stable_2020 for the time slots missing with respect to table calendar_recovery_interval_hour_2020

+------+---------------------+-----+| STUX | sdatetime           | sID |+------+---------------------+-----+|14|2020-04-1401:09:00|1||14|2020-04-1401:59:00|2||14|2020-04-1402:02:00|3||14|2020-04-1402:52:00|4|+------+---------------------+-----+4rowsinset

This the tutorial

On this example I need the output below, because on stable_2020 I have four rows

Two rows on time slot

|2020-04-1401:00:00|2020-04-1401:59:00|2|

Other two rows on time slot

|2020-04-1402:00:00|2020-04-1402:59:00|3|

output required

+---------------------+---------------------+| start_date          | end_date            |+---------------------+---------------------+|2020-04-1400:00:00|2020-04-1400:59:00||2020-04-1403:00:00|2020-04-1403:59:00||2020-04-1404:00:00|2020-04-1404:59:00||2020-04-1405:00:00|2020-04-1405:59:00||2020-04-1406:00:00|2020-04-1406:59:00||2020-04-1407:00:00|2020-04-1407:59:00||2020-04-1408:00:00|2020-04-1408:59:00||2020-04-1409:00:00|2020-04-1409:59:00||2020-04-1410:00:00|2020-04-1410:59:00||2020-04-1411:00:00|2020-04-1411:59:00||2020-04-1412:00:00|2020-04-1412:59:00||2020-04-1413:00:00|2020-04-1413:59:00||2020-04-1414:00:00|2020-04-1414:59:00||2020-04-1415:00:00|2020-04-1415:59:00||2020-04-1416:00:00|2020-04-1416:59:00||2020-04-1417:00:00|2020-04-1417:59:00||2020-04-1418:00:00|2020-04-1418:59:00||2020-04-1419:00:00|2020-04-1419:59:00||2020-04-1420:00:00|2020-04-1420:59:00||2020-04-1421:00:00|2020-04-1421:59:00||2020-04-1422:00:00|2020-04-1422:59:00||2020-04-1423:00:00|2020-04-1423:59:00|+---------------------+---------------------+

The code I've tried below without success

mysql>SELECTDISTINCT
    t.start_date,
    t.end_dateFROM`calendar_recovery_interval_hour_2020` t,`stable_2020` mWHERE
    m.`sdatetime`NOTBETWEEN Cast(t.start_date AS DateTime)AND Cast(t.end_date AS DateTime);+---------------------+---------------------+| start_date          | end_date            |+---------------------+---------------------+|2020-04-1400:00:00|2020-04-1400:59:00||2020-04-1401:00:00|2020-04-1401:59:00||2020-04-1402:00:00|2020-04-1402:59:00||2020-04-1403:00:00|2020-04-1403:59:00||2020-04-1404:00:00|2020-04-1404:59:00||2020-04-1405:00:00|2020-04-1405:59:00||2020-04-1406:00:00|2020-04-1406:59:00||2020-04-1407:00:00|2020-04-1407:59:00||2020-04-1408:00:00|2020-04-1408:59:00||2020-04-1409:00:00|2020-04-1409:59:00||2020-04-1410:00:00|2020-04-1410:59:00||2020-04-1411:00:00|2020-04-1411:59:00||2020-04-1412:00:00|2020-04-1412:59:00||2020-04-1413:00:00|2020-04-1413:59:00||2020-04-1414:00:00|2020-04-1414:59:00||2020-04-1415:00:00|2020-04-1415:59:00||2020-04-1416:00:00|2020-04-1416:59:00||2020-04-1417:00:00|2020-04-1417:59:00||2020-04-1418:00:00|2020-04-1418:59:00||2020-04-1419:00:00|2020-04-1419:59:00||2020-04-1420:00:00|2020-04-1420:59:00||2020-04-1421:00:00|2020-04-1421:59:00||2020-04-1422:00:00|2020-04-1422:59:00||2020-04-1423:00:00|2020-04-1423:59:00|+---------------------+---------------------+24rowsinset

And

mysql>SELECT
    start_date,
    end_dateFROM`calendar_recovery_interval_hour_2020` tWHEREEXISTS(SELECT1FROM`stable_2020` mWHERE
            m.`sdatetime`NOTBETWEEN t.start_dateAND t.end_dateORDERBY
            m.`sdatetime`DESC);+---------------------+---------------------+| start_date          | end_date            |+---------------------+---------------------+|2020-04-1400:00:00|2020-04-1400:59:00||2020-04-1401:00:00|2020-04-1401:59:00||2020-04-1402:00:00|2020-04-1402:59:00||2020-04-1403:00:00|2020-04-1403:59:00||2020-04-1404:00:00|2020-04-1404:59:00||2020-04-1405:00:00|2020-04-1405:59:00||2020-04-1406:00:00|2020-04-1406:59:00||2020-04-1407:00:00|2020-04-1407:59:00||2020-04-1408:00:00|2020-04-1408:59:00||2020-04-1409:00:00|2020-04-1409:59:00||2020-04-1410:00:00|2020-04-1410:59:00||2020-04-1411:00:00|2020-04-1411:59:00||2020-04-1412:00:00|2020-04-1412:59:00||2020-04-1413:00:00|2020-04-1413:59:00||2020-04-1414:00:00|2020-04-1414:59:00||2020-04-1415:00:00|2020-04-1415:59:00||2020-04-1416:00:00|2020-04-1416:59:00||2020-04-1417:00:00|2020-04-1417:59:00||2020-04-1418:00:00|2020-04-1418:59:00||2020-04-1419:00:00|2020-04-1419:59:00||2020-04-1420:00:00|2020-04-1420:59:00||2020-04-1421:00:00|2020-04-1421:59:00||2020-04-1422:00:00|2020-04-1422:59:00||2020-04-1423:00:00|2020-04-1423:59:00|+---------------------+---------------------+24rowsinset

My structure table below

DROPTABLEIFEXISTS`stable_2020`;CREATETABLE`stable_2020`(`STUX` int(11)DEFAULTNULL,`sdatetime` datetime DEFAULTNULL,`sID` int(11)NOTNULL AUTO_INCREMENT,PRIMARYKEY(`sID`),UNIQUEKEY`ukey`(`STUX`,`sdatetime`)USING BTREE,KEY`sdatetime`(`sdatetime`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;-- ------------------------------ Records of stable_2020-- ----------------------------INSERTINTO`stable_2020`VALUES('14','2020-04-14 01:09:00','1');INSERTINTO`stable_2020`VALUES('14','2020-04-14 01:59:00','2');INSERTINTO`stable_2020`VALUES('14','2020-04-14 02:02:00','3');INSERTINTO`stable_2020`VALUES('14','2020-04-14 02:52:00','4');DROPTABLEIFEXISTS`calendar_recovery_interval_hour_2020`;CREATETABLE`calendar_recovery_interval_hour_2020`(`start_date` datetime DEFAULT NUL`end_date` datetime DEFAULTNULL,`sID` int(11)NOTNULL AUTO_INCREMENT,PRIMARYKEY(`sID`)) ENGINE=InnoDB CHARSET=latin1;-- ------------------------------ Records of calendar_recovery_interval_hour_2020-- ----------------------------INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 00:00:00','2020-04-14 00:59:00','1');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 01:00:00','2020-04-14 01:59:00','2');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 02:00:00','2020-04-14 02:59:00','3');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 03:00:00','2020-04-14 03:59:00','4');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 04:00:00','2020-04-14 04:59:00','5');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 05:00:00','2020-04-14 05:59:00','6');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 06:00:00','2020-04-14 06:59:00','7');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 07:00:00','2020-04-14 07:59:00','8');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 08:00:00','2020-04-14 08:59:00','9');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 09:00:00','2020-04-14 09:59:00','10');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 10:00:00','2020-04-14 10:59:00','11');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 11:00:00','2020-04-14 11:59:00','12');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 12:00:00','2020-04-14 12:59:00','13');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 13:00:00','2020-04-14 13:59:00','14');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 14:00:00','2020-04-14 14:59:00','15');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 15:00:00','2020-04-14 15:59:00','16');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 16:00:00','2020-04-14 16:59:00','17');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 17:00:00','2020-04-14 17:59:00','18');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 18:00:00','2020-04-14 18:59:00','19');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 19:00:00','2020-04-14 19:59:00','20');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 20:00:00','2020-04-14 20:59:00','21');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 21:00:00','2020-04-14 21:59:00','22');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 22:00:00','2020-04-14 22:59:00','23');INSERTINTO`calendar_recovery_interval_hour_2020`VALUES('2020-04-14 23:00:00','2020-04-14 23:59:00','24');

Viewing all articles
Browse latest Browse all 1350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>