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
| --------------
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 `referencement`
--------------
--------------
CREATE TABLE `referencement`
( `id` integer unsigned not null auto_increment primary key,
`pays` integer unsigned not null,
`commune` varchar(255) not null,
`population` bigint unsigned not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `referencement` (`pays`,`commune`,`population`) values
(25, 'Paris', 1000000), (25, 'Lyon', 2000000), (25, 'Lille', 3000000), (25, 'Marseille', 1000000),
(25, 'Nice', 3000000), (25, 'Toulouse', 2000000), (25, 'Strasbourg', 3000000), (25, 'Bordeaux', 1000000),
(75, 'Rome', 2000000), (75, 'Venise', 3000000), (75, 'Naple', 1000000), (75, 'Turin', 2000000),
(75, 'Milan', 1000000), (75, 'Bologne', 3000000), (75, 'Florence', 3000000), (33, 'Madrid', 2000000),
(33, 'Séville', 3000000), (33, 'Grenande', 3000000), (33, 'Barcelone', 1000000)
--------------
--------------
select * from `referencement`
--------------
+----+------+------------+------------+
| id | pays | commune | population |
+----+------+------------+------------+
| 1 | 25 | Paris | 1000000 |
| 2 | 25 | Lyon | 2000000 |
| 3 | 25 | Lille | 3000000 |
| 4 | 25 | Marseille | 1000000 |
| 5 | 25 | Nice | 3000000 |
| 6 | 25 | Toulouse | 2000000 |
| 7 | 25 | Strasbourg | 3000000 |
| 8 | 25 | Bordeaux | 1000000 |
| 9 | 75 | Rome | 2000000 |
| 10 | 75 | Venise | 3000000 |
| 11 | 75 | Naple | 1000000 |
| 12 | 75 | Turin | 2000000 |
| 13 | 75 | Milan | 1000000 |
| 14 | 75 | Bologne | 3000000 |
| 15 | 75 | Florence | 3000000 |
| 16 | 33 | Madrid | 2000000 |
| 17 | 33 | Séville | 3000000 |
| 18 | 33 | Grenande | 3000000 |
| 19 | 33 | Barcelone | 1000000 |
+----+------+------------+------------+
--------------
drop table if exists `temp`
--------------
--------------
create table `temp`
( `id` integer unsigned not null auto_increment primary key,
`population` bigint unsigned not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `temp` (`population`) values (1000000),(2000000)
--------------
--------------
select * from `temp`
--------------
+----+------------+
| id | population |
+----+------------+
| 1 | 1000000 |
| 2 | 2000000 |
+----+------------+
--------------
drop view if exits `vue`
--------------
ERROR 1064 (42000) at line 77: Erreur de syntaxe près de 'exits `vue`' à la ligne 1
--------------
create view `vue` as
select r1.population,
r1.pays,
r1.commune,
count(case when r1.population = r2.population then 1 else 0 end) as rang
from `temp` as t
inner join `referencement` as r1
on r1.population = t.population
inner join `referencement` as r2
on r2.population = t.population
and r2.id < r1.id
group by r1.population, r1.pays, r1.commune
order by r1.population, r1.pays, r1.commune
--------------
--------------
select *
from vue
order by population, rang
--------------
+------------+------+-----------+------+
| population | pays | commune | rang |
+------------+------+-----------+------+
| 1000000 | 25 | Marseille | 1 |
| 1000000 | 25 | Bordeaux | 2 |
| 1000000 | 75 | Naple | 3 |
| 1000000 | 75 | Milan | 4 |
| 1000000 | 33 | Barcelone | 5 |
| 2000000 | 25 | Toulouse | 1 |
| 2000000 | 75 | Rome | 2 |
| 2000000 | 75 | Turin | 3 |
| 2000000 | 33 | Madrid | 4 |
+------------+------+-----------+------+
--------------
select *
from vue
where rang <= 3
order by population, rang
--------------
+------------+------+-----------+------+
| population | pays | commune | rang |
+------------+------+-----------+------+
| 1000000 | 25 | Marseille | 1 |
| 1000000 | 25 | Bordeaux | 2 |
| 1000000 | 75 | Naple | 3 |
| 2000000 | 25 | Toulouse | 1 |
| 2000000 | 75 | Rome | 2 |
| 2000000 | 75 | Turin | 3 |
+------------+------+-----------+------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager