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
| --------------
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 `tabone`
--------------
--------------
CREATE TABLE `tabone`
( `id` integer unsigned not null auto_increment primary key,
`code` char(03) not null,
`val` integer not null,
index `idx` (`code`)
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `tabone` (`code`,`val`) values
('12c',10),('34j',12),('56n',14),('78p',16),('90v',18),
('12d',20),('34f',22),('56o',24),('78q',26),('90w',28),
('12e',30),('34g',32),('56k',34),('78r',36),('90x',38),
('12a',40),('34h',42),('56l',44),('78s',46),('90y',48),
('12b',50),('34i',52),('56m',54),('78t',56),('90u',58)
--------------
--------------
select * from `tabone`
--------------
+----+------+-----+
| id | code | val |
+----+------+-----+
| 1 | 12c | 10 |
| 2 | 34j | 12 |
| 3 | 56n | 14 |
| 4 | 78p | 16 |
| 5 | 90v | 18 |
| 6 | 12d | 20 |
| 7 | 34f | 22 |
| 8 | 56o | 24 |
| 9 | 78q | 26 |
| 10 | 90w | 28 |
| 11 | 12e | 30 |
| 12 | 34g | 32 |
| 13 | 56k | 34 |
| 14 | 78r | 36 |
| 15 | 90x | 38 |
| 16 | 12a | 40 |
| 17 | 34h | 42 |
| 18 | 56l | 44 |
| 19 | 78s | 46 |
| 20 | 90y | 48 |
| 21 | 12b | 50 |
| 22 | 34i | 52 |
| 23 | 56m | 54 |
| 24 | 78t | 56 |
| 25 | 90u | 58 |
+----+------+-----+
--------------
DROP TABLE IF EXISTS `tabtwo`
--------------
--------------
CREATE TABLE `tabtwo`
( `id` integer unsigned not null auto_increment primary key,
`matricule` char(02) not null,
`mont` integer not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `tabtwo` (`matricule`,`mont`) values
('12',0),('34',0),('56',0),('78',0),('90',0)
--------------
--------------
select * from `tabtwo`
--------------
+----+-----------+------+
| id | matricule | mont |
+----+-----------+------+
| 1 | 12 | 0 |
| 2 | 34 | 0 |
| 3 | 56 | 0 |
| 4 | 78 | 0 |
| 5 | 90 | 0 |
+----+-----------+------+
--------------
update `tabtwo` as t1
inner join `tabone` as t2
on t2.code = (select min(code) from `tabone` as t3 where left(t3.code,2) = t1.matricule)
set t1.mont = t2.val
--------------
--------------
select * from `tabtwo`
--------------
+----+-----------+------+
| id | matricule | mont |
+----+-----------+------+
| 1 | 12 | 40 |
| 2 | 34 | 22 |
| 3 | 56 | 34 |
| 4 | 78 | 16 |
| 5 | 90 | 58 |
+----+-----------+------+
--------------
update `tabtwo` as t1
inner join (select left(code,2) as bis, any_value(val) as val from `tabone` group by bis) as t2
on t2.bis = t1.matricule
set t1.mont = t2.val
--------------
--------------
select * from `tabtwo`
--------------
+----+-----------+------+
| id | matricule | mont |
+----+-----------+------+
| 1 | 12 | 10 |
| 2 | 34 | 12 |
| 3 | 56 | 14 |
| 4 | 78 | 16 |
| 5 | 90 | 18 |
+----+-----------+------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager