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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| --------------
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 `produit`
--------------
--------------
CREATE TABLE `produit`
( `id` integer unsigned NOT NULL auto_increment primary key,
`nom` varchar(255) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `produit` (`id`,`nom`) values
(14, 'produit 14'),
(25, 'produit 25'),
(36, 'produit 36')
--------------
--------------
select * from `produit`
--------------
+----+------------+
| id | nom |
+----+------------+
| 14 | produit 14 |
| 25 | produit 25 |
| 36 | produit 36 |
+----+------------+
--------------
DROP TABLE IF EXISTS `date`
--------------
--------------
CREATE TABLE `date`
( `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
--------------
--------------
insert into `date` (`date`) values
('2010-01-03'), ('2010-05-08'), ('2011-01-25'), ('2011-02-06'), ('2011-02-24'), ('2012-12-12'),
('2014-05-06'), ('2015-01-12'), ('2016-01-10'), ('2016-12-12'), ('2017-05-15'), ('2017-05-20')
--------------
--------------
select * from `date`
--------------
+----+------------+
| id | date |
+----+------------+
| 1 | 2010-01-03 |
| 2 | 2010-05-08 |
| 3 | 2011-01-25 |
| 4 | 2011-02-06 |
| 5 | 2011-02-24 |
| 6 | 2012-12-12 |
| 7 | 2014-05-06 |
| 8 | 2015-01-12 |
| 9 | 2016-01-10 |
| 10 | 2016-12-12 |
| 11 | 2017-05-15 |
| 12 | 2017-05-20 |
+----+------------+
--------------
DROP TABLE IF EXISTS `commande`
--------------
--------------
CREATE TABLE `commande`
( `id_commande` integer unsigned NOT NULL auto_increment primary key,
`id_date` integer unsigned NOT NULL,
`id_produit` integer unsigned NOT NULL,
CONSTRAINT `FK_DATE` FOREIGN KEY (`id_date`) REFERENCES `date` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_PRODUIT` FOREIGN KEY (`id_produit`) REFERENCES `produit` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `commande` (`id_date`,`id_produit`) values
(01, 36), (02, 14), (03, 25), (04, 25), (05, 36), (06, 14),
(07, 14), (08, 25), (09, 36), (10, 36), (11, 25), (12, 14)
--------------
--------------
select * from `commande`
--------------
+-------------+---------+------------+
| id_commande | id_date | id_produit |
+-------------+---------+------------+
| 1 | 1 | 36 |
| 2 | 2 | 14 |
| 3 | 3 | 25 |
| 4 | 4 | 25 |
| 5 | 5 | 36 |
| 6 | 6 | 14 |
| 7 | 7 | 14 |
| 8 | 8 | 25 |
| 9 | 9 | 36 |
| 10 | 10 | 36 |
| 11 | 11 | 25 |
| 12 | 12 | 14 |
+-------------+---------+------------+
--------------
drop view if exists `vue`
--------------
--------------
create view `vue` as
select t1.id_produit as produit,
t2.nom,
t3.date as last_date,
t5.date as old_date,
count(t7.id) as rang
from `commande` as t1
inner join `produit` as t2
on t2.id = t1.id_produit
inner join `date` as t3
on t3.id = t1.id_date
inner join `commande` as t4
on t4.id_produit = t1.id_produit
inner join `date` as t5
on t5.id = t4.id_date
and t5.date < t3.date
left outer join `commande` as t6
on t6.id_produit = t1.id_produit
left outer join `date` as t7
on t7.id = t6.id_date
and t7.date > t5.date
where t3.date = ( select max(tb.date)
from `commande` as ta
inner join `date` as tb
on tb.id = ta.id_date
where ta.id_produit = t1.id_produit )
group by t1.id_produit, t2.nom, t3.date, t5.date
order by t1.id_produit, t2.nom, t3.date, t5.date desc
--------------
--------------
select * from vue
--------------
+---------+------------+------------+------------+------+
| produit | nom | last_date | old_date | rang |
+---------+------------+------------+------------+------+
| 14 | produit 14 | 2017-05-20 | 2014-05-06 | 1 |
| 14 | produit 14 | 2017-05-20 | 2012-12-12 | 2 |
| 14 | produit 14 | 2017-05-20 | 2010-05-08 | 3 |
| 25 | produit 25 | 2017-05-15 | 2015-01-12 | 1 |
| 25 | produit 25 | 2017-05-15 | 2011-02-06 | 2 |
| 25 | produit 25 | 2017-05-15 | 2011-01-25 | 3 |
| 36 | produit 36 | 2016-12-12 | 2016-01-10 | 1 |
| 36 | produit 36 | 2016-12-12 | 2011-02-24 | 2 |
| 36 | produit 36 | 2016-12-12 | 2010-01-03 | 3 |
+---------+------------+------------+------------+------+
--------------
select produit,
last_date as 'date de la commande',
old_date as 'date de l''ancienne commande'
from vue
where rang < 3
--------------
+---------+---------------------+-----------------------------+
| produit | date de la commande | date de l'ancienne commande |
+---------+---------------------+-----------------------------+
| 14 | 2017-05-20 | 2014-05-06 |
| 14 | 2017-05-20 | 2012-12-12 |
| 25 | 2017-05-15 | 2015-01-12 |
| 25 | 2017-05-15 | 2011-02-06 |
| 36 | 2016-12-12 | 2016-01-10 |
| 36 | 2016-12-12 | 2011-02-24 |
+---------+---------------------+-----------------------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager