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
| --------------
SET collation_connection = latin1_general_ci
--------------
--------------
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 `contact`
--------------
--------------
CREATE TABLE IF NOT EXISTS `contact`
( `id_contact` 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 `contact` (`nom`,`prenom`) values
('nom 3','prénom 3'),
('nom 2','prénom 2'),
('nom 4','prénom 4'),
('nom 1','prénom 1')
--------------
--------------
select * from `contact`
--------------
+------------+-------+----------+
| id_contact | nom | prenom |
+------------+-------+----------+
| 1 | nom 3 | prénom 3 |
| 2 | nom 2 | prénom 2 |
| 3 | nom 4 | prénom 4 |
| 4 | nom 1 | prénom 1 |
+------------+-------+----------+
--------------
DROP TABLE IF EXISTS `moyen_communication`
--------------
--------------
CREATE TABLE IF NOT EXISTS `moyen_communication`
( `id_communication` integer unsigned not null auto_increment primary key,
`tel` varchar(255) not null,
`type` varchar(255) not null
) engine=innoDB
default charset=latin1 collate=latin1_general_ci
row_format=compressed
--------------
--------------
insert into `moyen_communication` (`tel`,`type`) values
('12-34-56-78-90','professionel'),
('23-45-67-89-01','domicile'),
('34-45-67-89-01','domicile'),
('45-67-89-01-23','domicile'),
('56-78-90-12-34','professionel'),
('67-89-01-23-45','domicile'),
('78-90-12-34-56','professionel'),
('89-01-23-45-67','domicile'),
('90-12-34-56-78','professionel'),
('01-23-45-67-89','domicile'),
('98-76-54-32-10','professionnel'),
('76-54-32-10-98','domicile'),
('54-32-10-98-76','domicile'),
('32-10-98-76-54','domicile')
--------------
--------------
select * from `moyen_communication`
--------------
+------------------+----------------+---------------+
| id_communication | tel | type |
+------------------+----------------+---------------+
| 1 | 12-34-56-78-90 | professionel |
| 2 | 23-45-67-89-01 | domicile |
| 3 | 34-45-67-89-01 | domicile |
| 4 | 45-67-89-01-23 | domicile |
| 5 | 56-78-90-12-34 | professionel |
| 6 | 67-89-01-23-45 | domicile |
| 7 | 78-90-12-34-56 | professionel |
| 8 | 89-01-23-45-67 | domicile |
| 9 | 90-12-34-56-78 | professionel |
| 10 | 01-23-45-67-89 | domicile |
| 11 | 98-76-54-32-10 | professionnel |
| 12 | 76-54-32-10-98 | domicile |
| 13 | 54-32-10-98-76 | domicile |
| 14 | 32-10-98-76-54 | domicile |
+------------------+----------------+---------------+
--------------
DROP TABLE IF EXISTS `joindre_contact`
--------------
--------------
CREATE TABLE IF NOT EXISTS `joindre_contact`
( `id` integer unsigned not null auto_increment primary key,
`id_contact` integer unsigned not null,
`id_communication` integer unsigned not null,
CONSTRAINT `FK_CONTACT` FOREIGN KEY (`id_contact`) REFERENCES `contact` (`id_contact`) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT `FK_COMMUNICATION` FOREIGN KEY (`id_communication`) REFERENCES `moyen_communication` (`id_communication`) ON DELETE RESTRICT ON UPDATE CASCADE
) engine=innoDB
default charset=latin1 collate=latin1_general_ci
row_format=compressed
--------------
--------------
insert into `joindre_contact` (`id_contact`,`id_communication`) values
(1,1),(1,2),(1,3),(2,4),(2,5),(2,6),(3,7),(3,8),(3,9),(3,10),(4,11),(4,12),(4,13)
--------------
--------------
select * from `joindre_contact`
--------------
+----+------------+------------------+
| id | id_contact | id_communication |
+----+------------+------------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 2 | 4 |
| 5 | 2 | 5 |
| 6 | 2 | 6 |
| 7 | 3 | 7 |
| 8 | 3 | 8 |
| 9 | 3 | 9 |
| 10 | 3 | 10 |
| 11 | 4 | 11 |
| 12 | 4 | 12 |
| 13 | 4 | 13 |
+----+------------+------------------+
--------------
select id, rang, nom, prenom, type, tel
from (
select id,
@rang:=if(@prec=id, @rang:=@rang+1, 1) as rang,
@prec:= id as prec,
case @rang when 1 then nom else '' end as nom,
case @rang when 1 then prenom else '' end as prenom,
type,
tel
from (
select t2.id_contact as id,
t2.nom as nom,
t2.prenom as prenom,
t3.tel as tel,
t3.type as type
from `joindre_contact` as t1
inner join `contact` as t2
on t2.id_contact = t1.id_contact
inner join `moyen_communication` as t3
on t3.id_communication = t1.id_communication
order by t2.nom, t2.prenom, t3.type, t3.tel
) as x
cross join (select @rang:=0, @prec:='') as t4
) as y
--------------
+----+------+-------+----------+---------------+----------------+
| id | rang | nom | prenom | type | tel |
+----+------+-------+----------+---------------+----------------+
| 4 | 1 | nom 1 | prénom 1 | domicile | 54-32-10-98-76 |
| 4 | 2 | | | domicile | 76-54-32-10-98 |
| 4 | 3 | | | professionnel | 98-76-54-32-10 |
| 2 | 1 | nom 2 | prénom 2 | domicile | 45-67-89-01-23 |
| 2 | 2 | | | domicile | 67-89-01-23-45 |
| 2 | 3 | | | professionel | 56-78-90-12-34 |
| 1 | 1 | nom 3 | prénom 3 | domicile | 23-45-67-89-01 |
| 1 | 2 | | | domicile | 34-45-67-89-01 |
| 1 | 3 | | | professionel | 12-34-56-78-90 |
| 3 | 1 | nom 4 | prénom 4 | domicile | 01-23-45-67-89 |
| 3 | 2 | | | domicile | 89-01-23-45-67 |
| 3 | 3 | | | professionel | 78-90-12-34-56 |
| 3 | 4 | | | professionel | 90-12-34-56-78 |
+----+------+-------+----------+---------------+----------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager