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
| --------------
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 `membre`
--------------
--------------
CREATE TABLE `membre`
( `no_membre` integer unsigned not null auto_increment primary key,
`nom` varchar(255) not null,
`prenom` varchar(255) not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `membre` (`nom`,`prenom`) VALUES
('Nom 1','Prénom 1'),
('Nom 2','Prénom 2'),
('Nom 3','Prénom 3'),
('Nom 4','Prénom 4'),
('Nom 5','Prénom 5')
--------------
--------------
select * from `membre`
--------------
+-----------+-------+----------+
| no_membre | nom | prenom |
+-----------+-------+----------+
| 1 | Nom 1 | Prénom 1 |
| 2 | Nom 2 | Prénom 2 |
| 3 | Nom 3 | Prénom 3 |
| 4 | Nom 4 | Prénom 4 |
| 5 | Nom 5 | Prénom 5 |
+-----------+-------+----------+
--------------
DROP TABLE IF EXISTS `cotisation`
--------------
--------------
CREATE TABLE `cotisation`
( `id` integer unsigned not null auto_increment primary key,
`annee` smallint unsigned not null,
`no_membre` integer unsigned not null,
CONSTRAINT `FK_MEMBRE` FOREIGN KEY (`no_membre`) REFERENCES `membre` (`no_membre`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `cotisation` (`annee`,`no_membre`) VALUES
(2014,1),(2015,1),(2016,1),(2017,1),(2018,1),
(2015,2),(2017,2),(2018,2),
(2013,4),(2014,4),(2015,4),
(2018,5)
--------------
--------------
select * from `cotisation`
--------------
+----+-------+-----------+
| id | annee | no_membre |
+----+-------+-----------+
| 1 | 2014 | 1 |
| 2 | 2015 | 1 |
| 3 | 2016 | 1 |
| 4 | 2017 | 1 |
| 5 | 2018 | 1 |
| 6 | 2015 | 2 |
| 7 | 2017 | 2 |
| 8 | 2018 | 2 |
| 9 | 2013 | 4 |
| 10 | 2014 | 4 |
| 11 | 2015 | 4 |
| 12 | 2018 | 5 |
+----+-------+-----------+
--------------
select t1.no_membre,
max(t1.nom) as nom,
max(t1.prenom) as prenom,
case when t2.no_membre is not null then max(t2.annee) else '----' end as '2014',
case when t3.no_membre is not null then max(t3.annee) else '----' end as '2015',
case when t4.no_membre is not null then max(t4.annee) else '----' end as '2016',
case when t5.no_membre is not null then max(t5.annee) else '----' end as '2017',
case when t6.no_membre is not null then max(t6.annee) else '----' end as '2018'
from `membre` as t1
left outer join `cotisation` as t2
on t2.no_membre = t1.no_membre
and t2.annee = 2014
left outer join `cotisation` as t3
on t3.no_membre = t1.no_membre
and t3.annee = 2015
left outer join `cotisation` as t4
on t4.no_membre = t1.no_membre
and t4.annee = 2016
left outer join `cotisation` as t5
on t5.no_membre = t1.no_membre
and t5.annee = 2017
left outer join `cotisation` as t6
on t6.no_membre = t1.no_membre
and t6.annee = 2018
group by t1.no_membre
--------------
+-----------+-------+----------+------+------+------+------+------+
| no_membre | nom | prenom | 2014 | 2015 | 2016 | 2017 | 2018 |
+-----------+-------+----------+------+------+------+------+------+
| 1 | Nom 1 | Prénom 1 | 2014 | 2015 | 2016 | 2017 | 2018 |
| 2 | Nom 2 | Prénom 2 | ---- | 2015 | ---- | 2017 | 2018 |
| 5 | Nom 5 | Prénom 5 | ---- | ---- | ---- | ---- | 2018 |
| 4 | Nom 4 | Prénom 4 | 2014 | 2015 | ---- | ---- | ---- |
| 3 | Nom 3 | Prénom 3 | ---- | ---- | ---- | ---- | ---- |
+-----------+-------+----------+------+------+------+------+------+
--------------
select t1.no_membre,
max(t1.nom) as nom,
max(t1.prenom) as prenom,
max(case t2.annee when 2014 then '2014' else '----' end) as '2014',
max(case t2.annee when 2015 then '2015' else '----' end) as '2015',
max(case t2.annee when 2016 then '2016' else '----' end) as '2016',
max(case t2.annee when 2017 then '2017' else '----' end) as '2017',
max(case t2.annee when 2018 then '2018' else '----' end) as '2018'
from `membre` as t1
left outer join `cotisation` as t2
on t2.no_membre = t1.no_membre
group by t1.no_membre
--------------
+-----------+-------+----------+------+------+------+------+------+
| no_membre | nom | prenom | 2014 | 2015 | 2016 | 2017 | 2018 |
+-----------+-------+----------+------+------+------+------+------+
| 1 | Nom 1 | Prénom 1 | 2014 | 2015 | 2016 | 2017 | 2018 |
| 2 | Nom 2 | Prénom 2 | ---- | 2015 | ---- | 2017 | 2018 |
| 4 | Nom 4 | Prénom 4 | 2014 | 2015 | ---- | ---- | ---- |
| 5 | Nom 5 | Prénom 5 | ---- | ---- | ---- | ---- | 2018 |
| 3 | Nom 3 | Prénom 3 | ---- | ---- | ---- | ---- | ---- |
+-----------+-------+----------+------+------+------+------+------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager