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 215 216 217
| --------------
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 `membres`
--------------
--------------
CREATE TABLE `membres`
(
`id` integer unsigned NOT NULL AUTO_INCREMENT,
`nom` varchar(255) NOT NULL,
`nbmatchs` integer NOT NULL,
`nbpoints` integer NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO membres (`nom`,`nbmatchs`,`nbpoints`) VALUES
('albert', 0, 0),
('maurice', 0, 0),
('leon', 0, 0),
('patrick', 0, 0)
--------------
--------------
select * from membres
--------------
+----+---------+----------+----------+
| id | nom | nbmatchs | nbpoints |
+----+---------+----------+----------+
| 1 | albert | 0 | 0 |
| 2 | maurice | 0 | 0 |
| 3 | leon | 0 | 0 |
| 4 | patrick | 0 | 0 |
+----+---------+----------+----------+
--------------
DROP TABLE IF EXISTS `matchs`
--------------
--------------
CREATE TABLE `matchs`
(
`id` integer unsigned NOT NULL AUTO_INCREMENT,
`nomgagnant` varchar(255) NOT NULL,
`nomperdant` varchar(255) NOT NULL,
`pointgagnant` integer NOT NULL,
`pointperdant` integer NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO matchs (`nomgagnant`,`nomperdant`,`pointgagnant`,`pointperdant`) VALUES
('leon', 'patrick', 2, 1),
('patrick', 'albert', 5, 4),
('albert', 'maurice', 3, 2),
('maurice', 'leon', 4, 2)
--------------
--------------
select * from matchs
--------------
+----+------------+------------+--------------+--------------+
| id | nomgagnant | nomperdant | pointgagnant | pointperdant |
+----+------------+------------+--------------+--------------+
| 1 | leon | patrick | 2 | 1 |
| 2 | patrick | albert | 5 | 4 |
| 3 | albert | maurice | 3 | 2 |
| 4 | maurice | leon | 4 | 2 |
+----+------------+------------+--------------+--------------+
--------------
create temporary table temp1 (
`nom` varchar(255) NOT NULL,
`plus` integer NOT NULL,
`moins` integer NOT NULL
)
--------------
--------------
insert into temp1
select nom,
sum(plus) as plus,
sum(moins) as moins
from (
select nomgagnant as nom,
sum(pointgagnant) as plus,
sum(pointperdant) as moins
from matchs
group by nomgagnant
union all
select nomperdant as nom,
sum(pointgagnant) as plus,
sum(pointperdant) as moins
from matchs
group by nomperdant
) as x
group by nom
--------------
--------------
select * from temp1
--------------
+---------+------+-------+
| nom | plus | moins |
+---------+------+-------+
| albert | 8 | 6 |
| leon | 6 | 3 |
| maurice | 7 | 4 |
| patrick | 7 | 5 |
+---------+------+-------+
--------------
update membres as m, temp1 as t
set m.nbpoints = m.nbpoints + t.plus + t.moins
where m.nom = t.nom
--------------
--------------
select * from membres
--------------
+----+---------+----------+----------+
| id | nom | nbmatchs | nbpoints |
+----+---------+----------+----------+
| 1 | albert | 0 | 14 |
| 2 | maurice | 0 | 11 |
| 3 | leon | 0 | 9 |
| 4 | patrick | 0 | 12 |
+----+---------+----------+----------+
--------------
create temporary table temp2 (
`nom` varchar(255) NOT NULL,
`nbre` integer NOT NULL
)
--------------
--------------
insert into temp2
select nom,
sum(nbre) as nbre
from (
select nomgagnant as nom,
count(*) as nbre
from matchs
group by nomgagnant
union all
select nomperdant as nom,
count(*) as nbre
from matchs
group by nomperdant
) as x
group by nom
--------------
--------------
select * from temp2
--------------
+---------+------+
| nom | nbre |
+---------+------+
| albert | 2 |
| leon | 2 |
| maurice | 2 |
| patrick | 2 |
+---------+------+
--------------
update membres as m, temp2 as t
set m.nbmatchs = t.nbre
where m.nom = t.nom
--------------
--------------
select * from membres
--------------
+----+---------+----------+----------+
| id | nom | nbmatchs | nbpoints |
+----+---------+----------+----------+
| 1 | albert | 2 | 14 |
| 2 | maurice | 2 | 11 |
| 3 | leon | 2 | 9 |
| 4 | patrick | 2 | 12 |
+----+---------+----------+----------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager