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
| --------------
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 `alimente`
--------------
--------------
CREATE TABLE `alimente`
( `id_alimente` integer unsigned NOT NULL auto_increment primary key,
`id_aquarium` integer unsigned NOT NULL,
`fin_alimente` date NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `alimente` (`id_aquarium`,`fin_alimente`) values
(1,'2016-08-01'),(1,'2016-08-02'),(1,'2016-08-23'),(1,'2016-08-15'),(1,'2016-08-23'),(1,'2016-08-18'),
(2,'2016-07-21'),(2,'2016-07-15'),(2,'2016-07-21'),(2,'2016-07-18'),(2,'2016-07-14')
--------------
--------------
select * from alimente
--------------
+-------------+-------------+--------------+
| id_alimente | id_aquarium | fin_alimente |
+-------------+-------------+--------------+
| 1 | 1 | 2016-08-01 |
| 2 | 1 | 2016-08-02 |
| 3 | 1 | 2016-08-23 |
| 4 | 1 | 2016-08-15 |
| 5 | 1 | 2016-08-23 |
| 6 | 1 | 2016-08-18 |
| 7 | 2 | 2016-07-21 |
| 8 | 2 | 2016-07-15 |
| 9 | 2 | 2016-07-21 |
| 10 | 2 | 2016-07-18 |
| 11 | 2 | 2016-07-14 |
+-------------+-------------+--------------+
--------------
select max(t2.fin_alimente) as fin_alimente
from alimente as t2
group by t2.id_aquarium
--------------
+--------------+
| fin_alimente |
+--------------+
| 2016-08-23 |
| 2016-07-21 |
+--------------+
--------------
select *
from alimente as t1
where fin_alimente = (
select max(t2.fin_alimente) as fin_alimente
from alimente as t2
where t2.id_aquarium = t1.id_aquarium
)
--------------
+-------------+-------------+--------------+
| id_alimente | id_aquarium | fin_alimente |
+-------------+-------------+--------------+
| 3 | 1 | 2016-08-23 |
| 5 | 1 | 2016-08-23 |
| 7 | 2 | 2016-07-21 |
| 9 | 2 | 2016-07-21 |
+-------------+-------------+--------------+
--------------
select *
from alimente as t1
where id_alimente = (
select t2.id_alimente
from alimente as t2
left outer join alimente as t3
on t3.id_aquarium = t2.id_aquarium
and (( t3.fin_alimente > t2.fin_alimente )
or ((t3.fin_alimente = t2.fin_alimente)
and (t3.id_alimente > t2.id_alimente )))
where t3.id_alimente is null
and t2.id_aquarium = t1.id_aquarium
group by t3.id_aquarium
)
--------------
+-------------+-------------+--------------+
| id_alimente | id_aquarium | fin_alimente |
+-------------+-------------+--------------+
| 5 | 1 | 2016-08-23 |
| 9 | 2 | 2016-07-21 |
+-------------+-------------+--------------+
--------------
select a1.*
from alimente as a1
left outer join alimente as a2 on a2.id_aquarium = a1.id_aquarium and a2.fin_alimente > a1.fin_alimente
left outer join alimente as a3 on a3.id_aquarium = a1.id_aquarium and a3.fin_alimente = a1.fin_alimente and a3.id_alimente > a1.id_alimente
where a2.fin_alimente is null and a3.id_alimente is null
--------------
+-------------+-------------+--------------+
| id_alimente | id_aquarium | fin_alimente |
+-------------+-------------+--------------+
| 5 | 1 | 2016-08-23 |
| 9 | 2 | 2016-07-21 |
+-------------+-------------+--------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager