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
| --------------
SET AUTOCOMMIT = 0
--------------
--------------
START TRANSACTION
--------------
--------------
SET collation_connection = latin1_general_ci
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `tablebase`
--------------
--------------
CREATE TABLE `tablebase`
( `id` integer unsigned not null auto_increment primary key,
`datebase` date not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `tablebase` (`datebase`) values
('2017-01-01'),('2017-01-02'),('2017-01-04')
--------------
--------------
select * from tablebase
--------------
+----+------------+
| id | datebase |
+----+------------+
| 1 | 2017-01-01 |
| 2 | 2017-01-02 |
| 3 | 2017-01-04 |
+----+------------+
--------------
DROP TABLE IF EXISTS `tabletest`
--------------
--------------
CREATE TABLE `tabletest`
( `id` integer unsigned not null auto_increment primary key,
`clef` integer unsigned not null,
`datetest` date not null,
`texte` char(04) not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `tabletest` (`clef`, `datetest`, `texte`) VALUES
(1, '2017-01-18', 'lol'),
(1, '2017-01-18', 'vrai'),
(1, '2017-01-27', 'faux'),
(2, '2017-01-07', 'faux'),
(2, '2017-01-20', 'vrai'),
(3, '2017-01-19', 'hein'),
(3, '2017-01-11', 'faux'),
(3, '2017-01-15', 'vrai'),
(3, '2017-01-14', 'vrai'),
(1, '2017-01-28', 'hein')
--------------
--------------
select * from tabletest
--------------
+----+------+------------+-------+
| id | clef | datetest | texte |
+----+------+------------+-------+
| 1 | 1 | 2017-01-18 | lol |
| 2 | 1 | 2017-01-18 | vrai |
| 3 | 1 | 2017-01-27 | faux |
| 4 | 2 | 2017-01-07 | faux |
| 5 | 2 | 2017-01-20 | vrai |
| 6 | 3 | 2017-01-19 | hein |
| 7 | 3 | 2017-01-11 | faux |
| 8 | 3 | 2017-01-15 | vrai |
| 9 | 3 | 2017-01-14 | vrai |
| 10 | 1 | 2017-01-28 | hein |
+----+------+------------+-------+
--------------
select *,
DATEDIFF(t.datetest, b.datebase) as diff
from tabletest as t
inner join tablebase as b
on b.id = t.clef
order by b.id, t.id
--------------
+----+------+------------+-------+----+------------+------+
| id | clef | datetest | texte | id | datebase | diff |
+----+------+------------+-------+----+------------+------+
| 1 | 1 | 2017-01-18 | lol | 1 | 2017-01-01 | 17 |
| 2 | 1 | 2017-01-18 | vrai | 1 | 2017-01-01 | 17 |
| 3 | 1 | 2017-01-27 | faux | 1 | 2017-01-01 | 26 |
| 10 | 1 | 2017-01-28 | hein | 1 | 2017-01-01 | 27 |
| 4 | 2 | 2017-01-07 | faux | 2 | 2017-01-02 | 5 |
| 5 | 2 | 2017-01-20 | vrai | 2 | 2017-01-02 | 18 |
| 6 | 3 | 2017-01-19 | hein | 3 | 2017-01-04 | 15 |
| 7 | 3 | 2017-01-11 | faux | 3 | 2017-01-04 | 7 |
| 8 | 3 | 2017-01-15 | vrai | 3 | 2017-01-04 | 11 |
| 9 | 3 | 2017-01-14 | vrai | 3 | 2017-01-04 | 10 |
+----+------+------------+-------+----+------------+------+
--------------
select *,
DATEDIFF(t.datetest, b.datebase) as diff
from tabletest as t
inner join tablebase as b
on b.id = t.clef
where t.id = ( select max(id)
from tabletest as z
where z.clef = t.clef)
order by b.id, t.id
--------------
+----+------+------------+-------+----+------------+------+
| id | clef | datetest | texte | id | datebase | diff |
+----+------+------------+-------+----+------------+------+
| 10 | 1 | 2017-01-28 | hein | 1 | 2017-01-01 | 27 |
| 5 | 2 | 2017-01-20 | vrai | 2 | 2017-01-02 | 18 |
| 9 | 3 | 2017-01-14 | vrai | 3 | 2017-01-04 | 10 |
+----+------+------------+-------+----+------------+------+
--------------
select *,
DATEDIFF(t.datetest, b.datebase) as diff
from tabletest as t
inner join tablebase as b
on b.id = t.clef
where t.id = ( select max(id)
from tabletest as z
where z.clef = t.clef)
and t.texte = 'vrai'
order by b.id, t.id
--------------
+----+------+------------+-------+----+------------+------+
| id | clef | datetest | texte | id | datebase | diff |
+----+------+------------+-------+----+------------+------+
| 5 | 2 | 2017-01-20 | vrai | 2 | 2017-01-02 | 18 |
| 9 | 3 | 2017-01-14 | vrai | 3 | 2017-01-04 | 10 |
+----+------+------------+-------+----+------------+------+
--------------
select sum(DATEDIFF(t.datetest, b.datebase)) as cumul
from tabletest as t
inner join tablebase as b
on b.id = t.clef
where t.id = ( select max(id)
from tabletest as z
where z.clef = t.clef)
and t.texte = 'vrai'
order by b.id, t.id
--------------
+-------+
| cumul |
+-------+
| 28 |
+-------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager