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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| --------------
SET AUTOCOMMIT = 0
--------------
--------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `calendrier`
--------------
--------------
CREATE TABLE `calendrier`
( `id` integer unsigned not null auto_increment primary key,
`date` date not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
DROP PROCEDURE IF EXISTS `remplir`
--------------
--------------
CREATE PROCEDURE `remplir` (date_deb date,
date_fin date)
DETERMINISTIC
NO SQL
BEGIN
DECLARE _date date default null;
SET _date = date_deb;
WHILE (_date <= date_fin)
DO
insert into `calendrier` (`date`) value (_date);
set _date = _date + interval 1 day;
END WHILE;
END
--------------
--------------
call `remplir` ('2018-03-01', '2018-03-10')
--------------
--------------
select * from `calendrier`
--------------
+----+------------+
| id | date |
+----+------------+
| 1 | 2018-03-01 |
| 2 | 2018-03-02 |
| 3 | 2018-03-03 |
| 4 | 2018-03-04 |
| 5 | 2018-03-05 |
| 6 | 2018-03-06 |
| 7 | 2018-03-07 |
| 8 | 2018-03-08 |
| 9 | 2018-03-09 |
| 10 | 2018-03-10 |
+----+------------+
--------------
DROP TABLE IF EXISTS `test`
--------------
--------------
CREATE TABLE `test`
( `id` integer unsigned not null auto_increment primary key,
`periode` date not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `test` (`periode`) values
('2018-03-01'),
('2018-03-03'),
('2018-03-07'),
('2018-03-05'),
('2018-03-07'),
('2018-03-03'),
('2018-03-09')
--------------
--------------
select * from `test`
--------------
+----+------------+
| id | periode |
+----+------------+
| 1 | 2018-03-01 |
| 2 | 2018-03-03 |
| 3 | 2018-03-07 |
| 4 | 2018-03-05 |
| 5 | 2018-03-07 |
| 6 | 2018-03-03 |
| 7 | 2018-03-09 |
+----+------------+
--------------
commit
--------------
--------------
select t1.date,
count(t2.periode) as nbre
from `calendrier` as t1
left outer join `test` as t2
on t2.periode = t1.date
group by t1.date
order by t1.date
--------------
+------------+------+
| date | nbre |
+------------+------+
| 2018-03-01 | 1 |
| 2018-03-02 | 0 |
| 2018-03-03 | 2 |
| 2018-03-04 | 0 |
| 2018-03-05 | 1 |
| 2018-03-06 | 0 |
| 2018-03-07 | 2 |
| 2018-03-08 | 0 |
| 2018-03-09 | 1 |
| 2018-03-10 | 0 |
+------------+------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager