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
| --------------
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 `historique`
--------------
--------------
CREATE TABLE `historique`
( `id` integer unsigned NOT NULL auto_increment primary key,
`produit` integer unsigned NOT NULL,
`date` date NOT NULL,
`prix` decimal(5,2) NOT NULL,
index `idx` (`produit`,`date`)
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `historique` (`produit`,`date`,`prix`) VALUES
(1,'2018-01-05', 10.00),
(1,'2018-02-05', 15.00),
(2,'2018-02-05', 100.00),
(2,'2018-04-05', 150.00)
--------------
--------------
select * from `historique`
--------------
+----+---------+------------+--------+
| id | produit | date | prix |
+----+---------+------------+--------+
| 1 | 1 | 2018-01-05 | 10.00 |
| 2 | 1 | 2018-02-05 | 15.00 |
| 3 | 2 | 2018-02-05 | 100.00 |
| 4 | 2 | 2018-04-05 | 150.00 |
+----+---------+------------+--------+
--------------
DROP TABLE IF EXISTS `test`
--------------
--------------
CREATE TABLE `test`
( `id` integer unsigned NOT NULL auto_increment primary key,
`produit` integer unsigned NOT NULL,
`achat` date NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `test` (`produit`,`achat`) VALUES
(1,'2018-01-01'),(2,'2018-01-01'),
(1,'2018-01-25'),(2,'2018-03-25'),
(1,'2018-02-25'),(2,'2018-04-25')
--------------
--------------
select * from `test`
--------------
+----+---------+------------+
| id | produit | achat |
+----+---------+------------+
| 1 | 1 | 2018-01-01 |
| 2 | 2 | 2018-01-01 |
| 3 | 1 | 2018-01-25 |
| 4 | 2 | 2018-03-25 |
| 5 | 1 | 2018-02-25 |
| 6 | 2 | 2018-04-25 |
+----+---------+------------+
--------------
select t1.produit,
t1.achat,
t2.date,
t2.prix
from `test` as t1
left outer join `historique` as t2
on t2.produit = t1.produit
and t2.date = ( select max(t3.date)
from `historique` as t3
where t3.date < t1.achat
)
--------------
+---------+------------+------------+--------+
| produit | achat | date | prix |
+---------+------------+------------+--------+
| 1 | 2018-01-01 | NULL | NULL |
| 2 | 2018-01-01 | NULL | NULL |
| 1 | 2018-01-25 | 2018-01-05 | 10.00 |
| 2 | 2018-03-25 | 2018-02-05 | 100.00 |
| 1 | 2018-02-25 | 2018-02-05 | 15.00 |
| 2 | 2018-04-25 | 2018-04-05 | 150.00 |
+---------+------------+------------+--------+
--------------
COMMIT
--------------
Appuyez sur une touche pour continuer... |
Partager