1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| --------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE IF NOT EXISTS `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `test`
--------------
--------------
CREATE TABLE `test`
( `id` integer unsigned NOT NULL auto_increment primary key,
`nom_usuel` varchar(255) NOT NULL,
`date_debut_abs` date NOT NULL,
`date_fin_abs` date NOT NULL,
`nb_jour` smallint unsigned NOT NULL,
`type_v` char(02) NOT NULL,
`date_ajouter` date NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `test` (`nom_usuel`,`date_debut_abs`,`date_fin_abs`,`nb_jour`,`type_v`,`date_ajouter`) values
('Paris', '2017-06-15', '2017-06-15', 1, 'V1', '2017-06-16'),
('Paris', '2017-06-21', '2017-06-21', 1, 'V1', '2017-06-22'),
('Paris', '2017-07-04', '2017-07-04', 1, 'V1', '2017-07-05'),
('Paris', '2017-07-05', '2017-07-05', 1, 'V1', '2017-07-06'),
('Paris', '2017-07-14', '2017-07-20', 6, 'V2', '2017-07-15'),
('Paris', '2017-08-16', '2017-08-31', 15, 'V2', '2017-08-17'),
('Paris', '2017-09-01', '2017-09-02', 1, 'V2', '2017-09-02'),
('Paris', '2017-09-29', '2017-09-29', 1, 'V1', '2017-09-30'),
('Paris', '2017-09-30', '2017-09-30', 1, 'V1', '2017-10-01')
--------------
--------------
select * from `test`
--------------
+----+-----------+----------------+--------------+---------+--------+--------------+
| id | nom_usuel | date_debut_abs | date_fin_abs | nb_jour | type_v | date_ajouter |
+----+-----------+----------------+--------------+---------+--------+--------------+
| 1 | Paris | 2017-06-15 | 2017-06-15 | 1 | V1 | 2017-06-16 |
| 2 | Paris | 2017-06-21 | 2017-06-21 | 1 | V1 | 2017-06-22 |
| 3 | Paris | 2017-07-04 | 2017-07-04 | 1 | V1 | 2017-07-05 |
| 4 | Paris | 2017-07-05 | 2017-07-05 | 1 | V1 | 2017-07-06 |
| 5 | Paris | 2017-07-14 | 2017-07-20 | 6 | V2 | 2017-07-15 |
| 6 | Paris | 2017-08-16 | 2017-08-31 | 15 | V2 | 2017-08-17 |
| 7 | Paris | 2017-09-01 | 2017-09-02 | 1 | V2 | 2017-09-02 |
| 8 | Paris | 2017-09-29 | 2017-09-29 | 1 | V1 | 2017-09-30 |
| 9 | Paris | 2017-09-30 | 2017-09-30 | 1 | V1 | 2017-10-01 |
+----+-----------+----------------+--------------+---------+--------+--------------+
--------------
select t1.id,
t1.date_debut_abs,
t1.date_fin_abs,
t2.id,
t2.date_debut_abs,
t2.date_fin_abs,
case when t2.date_debut_abs = date_add(t1.date_fin_abs, interval +1 day) then 'ok' else 'ko' end as test
from `test` as t1
left outer join `test` as t2
on t2.id = ( select min(id)
from `test` as t3
where t3.id > t1.id
)
--------------
+----+----------------+--------------+------+----------------+--------------+------+
| id | date_debut_abs | date_fin_abs | id | date_debut_abs | date_fin_abs | test |
+----+----------------+--------------+------+----------------+--------------+------+
| 1 | 2017-06-15 | 2017-06-15 | 2 | 2017-06-21 | 2017-06-21 | ko |
| 2 | 2017-06-21 | 2017-06-21 | 3 | 2017-07-04 | 2017-07-04 | ko |
| 3 | 2017-07-04 | 2017-07-04 | 4 | 2017-07-05 | 2017-07-05 | ok |
| 4 | 2017-07-05 | 2017-07-05 | 5 | 2017-07-14 | 2017-07-20 | ko |
| 5 | 2017-07-14 | 2017-07-20 | 6 | 2017-08-16 | 2017-08-31 | ko |
| 6 | 2017-08-16 | 2017-08-31 | 7 | 2017-09-01 | 2017-09-02 | ok |
| 7 | 2017-09-01 | 2017-09-02 | 8 | 2017-09-29 | 2017-09-29 | ko |
| 8 | 2017-09-29 | 2017-09-29 | 9 | 2017-09-30 | 2017-09-30 | ok |
| 9 | 2017-09-30 | 2017-09-30 | NULL | NULL | NULL | ko |
+----+----------------+--------------+------+----------------+--------------+------+
--------------
COMMIT
--------------
Appuyez sur une touche pour continuer... |