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
| --------------
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 `test`
--------------
--------------
CREATE TABLE `test`
( `id` integer unsigned not null auto_increment primary key,
`id_nom` integer unsigned not null,
`nom` varchar(255) not null,
`email` varchar(255) not null
) engine=innoDB
default charset=latin1 collate=latin1_general_ci
row_format=compressed
--------------
--------------
insert into `test` (`id`,`id_nom`,`nom`,`email`) values
( 1, 552, 'paul', ''),
( 2, 4298, '', 'jean@jean.fr'),
(203, 552, '', 'paul@paul.fr'),
(352, 4298, 'jean', '')
--------------
--------------
select * from test
--------------
+-----+--------+------+--------------+
| id | id_nom | nom | email |
+-----+--------+------+--------------+
| 1 | 552 | paul | |
| 2 | 4298 | | jean@jean.fr |
| 203 | 552 | | paul@paul.fr |
| 352 | 4298 | jean | |
+-----+--------+------+--------------+
--------------
update `test` as t1
inner join `test` as t2
on t2.id_nom = t1.id_nom
set t1.nom = t2.nom
where t1.nom = ''
and t2.nom != ''
--------------
--------------
select * from test
--------------
+-----+--------+------+--------------+
| id | id_nom | nom | email |
+-----+--------+------+--------------+
| 1 | 552 | paul | |
| 2 | 4298 | jean | jean@jean.fr |
| 203 | 552 | paul | paul@paul.fr |
| 352 | 4298 | jean | |
+-----+--------+------+--------------+
--------------
update `test` as t1
inner join `test` as t2
on t2.id_nom = t1.id_nom
set t1.email = t2.email
where t1.email = ''
and t2.email != ''
--------------
--------------
select * from test
--------------
+-----+--------+------+--------------+
| id | id_nom | nom | email |
+-----+--------+------+--------------+
| 1 | 552 | paul | paul@paul.fr |
| 2 | 4298 | jean | jean@jean.fr |
| 203 | 552 | paul | paul@paul.fr |
| 352 | 4298 | jean | jean@jean.fr |
+-----+--------+------+--------------+
--------------
delete from `test`
where id not in (select id from (select min(id) as id from test group by id_nom) as x)
--------------
--------------
select * from test
--------------
+----+--------+------+--------------+
| id | id_nom | nom | email |
+----+--------+------+--------------+
| 1 | 552 | paul | paul@paul.fr |
| 2 | 4298 | jean | jean@jean.fr |
+----+--------+------+--------------+
--------------
commit
--------------
--------------
set autocommit = 1
--------------
Appuyez sur une touche pour continuer... |
Partager