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 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| --------------
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 `artstock`
--------------
--------------
CREATE TABLE `artstock`
( `id` integer unsigned not null auto_increment primary key,
`article` char(10) not null,
`emplacement` char(10) not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `artstock` (`article`,`emplacement`) values
('Ref1','Lieu1'), ('Ref1','Lieu2'), ('Ref1','Lieu3'), ('Ref1','Lieu4'), ('Ref2','Lieu1'), ('Ref2','Lieu5')
--------------
--------------
select * from artstock
--------------
+----+---------+-------------+
| id | article | emplacement |
+----+---------+-------------+
| 1 | Ref1 | Lieu1 |
| 2 | Ref1 | Lieu2 |
| 3 | Ref1 | Lieu3 |
| 4 | Ref1 | Lieu4 |
| 5 | Ref2 | Lieu1 |
| 6 | Ref2 | Lieu5 |
+----+---------+-------------+
--------------
DROP TABLE IF EXISTS `artlieu`
--------------
--------------
CREATE TABLE `artlieu`
( `id` integer unsigned not null auto_increment primary key,
`article` char(10) not null,
`emplacement` char(10) not null,
`quantite` integer unsigned not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `artlieu` (`article`,`emplacement`,`quantite`) values
('Ref1','Lieu1',1), ('Ref1','LieuA',2), ('Ref1','LieuB',3), ('Ref2','Lieu1',4), ('Ref2','LieuC',5)
--------------
--------------
select * from artlieu
--------------
+----+---------+-------------+----------+
| id | article | emplacement | quantite |
+----+---------+-------------+----------+
| 1 | Ref1 | Lieu1 | 1 |
| 2 | Ref1 | LieuA | 2 |
| 3 | Ref1 | LieuB | 3 |
| 4 | Ref2 | Lieu1 | 4 |
| 5 | Ref2 | LieuC | 5 |
+----+---------+-------------+----------+
--------------
create table `sample` as
select s.article as 'article',
s.emplacement as 'stock_emplacement',
l.emplacement as 'lieu_emplacement',
l.quantite as 'quantite',
s.id as 'ident_stock',
l.id as 'ident_lieu'
from artstock as s
left outer join artlieu as l
on l.article = s.article
and l.emplacement = s.emplacement
union
select l.article as 'article',
s.emplacement as 'stock_emplacement',
l.emplacement as 'lieu_emplacement',
l.quantite as 'quantite',
s.id as 'ident_stock',
l.id as 'ident_lieu'
from artlieu as l
left outer join artstock as s
on s.article = l.article
and s.emplacement = l.emplacement
order by article, stock_emplacement, lieu_emplacement
--------------
--------------
select * from sample
--------------
+---------+-------------------+------------------+----------+-------------+------------+
| article | stock_emplacement | lieu_emplacement | quantite | ident_stock | ident_lieu |
+---------+-------------------+------------------+----------+-------------+------------+
| Ref1 | NULL | LieuA | 2 | NULL | 2 |
| Ref1 | NULL | LieuB | 3 | NULL | 3 |
| Ref1 | Lieu1 | Lieu1 | 1 | 1 | 1 |
| Ref1 | Lieu2 | NULL | NULL | 2 | NULL |
| Ref1 | Lieu3 | NULL | NULL | 3 | NULL |
| Ref1 | Lieu4 | NULL | NULL | 4 | NULL |
| Ref2 | NULL | LieuC | 5 | NULL | 5 |
| Ref2 | Lieu1 | Lieu1 | 4 | 5 | 4 |
| Ref2 | Lieu5 | NULL | NULL | 6 | NULL |
+---------+-------------------+------------------+----------+-------------+------------+
--------------
select *
from sample
where quantite is null
--------------
+---------+-------------------+------------------+----------+-------------+------------+
| article | stock_emplacement | lieu_emplacement | quantite | ident_stock | ident_lieu |
+---------+-------------------+------------------+----------+-------------+------------+
| Ref1 | Lieu2 | NULL | NULL | 2 | NULL |
| Ref1 | Lieu3 | NULL | NULL | 3 | NULL |
| Ref1 | Lieu4 | NULL | NULL | 4 | NULL |
| Ref2 | Lieu5 | NULL | NULL | 6 | NULL |
+---------+-------------------+------------------+----------+-------------+------------+
--------------
delete t1 from `artstock` as t1
inner join `sample` as t2
on t2.article = t1.article
and t2.stock_emplacement = t1.emplacement
where t2.quantite is null
--------------
--------------
select * from artstock
--------------
+----+---------+-------------+
| id | article | emplacement |
+----+---------+-------------+
| 1 | Ref1 | Lieu1 |
| 5 | Ref2 | Lieu1 |
+----+---------+-------------+
--------------
select *
from sample
where stock_emplacement is null
--------------
+---------+-------------------+------------------+----------+-------------+------------+
| article | stock_emplacement | lieu_emplacement | quantite | ident_stock | ident_lieu |
+---------+-------------------+------------------+----------+-------------+------------+
| Ref1 | NULL | LieuA | 2 | NULL | 2 |
| Ref1 | NULL | LieuB | 3 | NULL | 3 |
| Ref2 | NULL | LieuC | 5 | NULL | 5 |
+---------+-------------------+------------------+----------+-------------+------------+
--------------
insert into `artstock` (`article`,`emplacement`)
select article,
lieu_emplacement as emplacement
from sample
where stock_emplacement is null
--------------
--------------
select * from artstock
--------------
+----+---------+-------------+
| id | article | emplacement |
+----+---------+-------------+
| 1 | Ref1 | Lieu1 |
| 5 | Ref2 | Lieu1 |
| 7 | Ref1 | LieuA |
| 8 | Ref1 | LieuB |
| 9 | Ref2 | LieuC |
+----+---------+-------------+
--------------
alter table `artstock` drop column `id`
--------------
--------------
alter table `artstock` add column `id` integer unsigned auto_increment not null primary key first
--------------
--------------
select * from artstock
--------------
+----+---------+-------------+
| id | article | emplacement |
+----+---------+-------------+
| 1 | Ref1 | Lieu1 |
| 2 | Ref2 | Lieu1 |
| 3 | Ref1 | LieuA |
| 4 | Ref1 | LieuB |
| 5 | Ref2 | LieuC |
+----+---------+-------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager