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
| --------------
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,
`identifiant` varchar(255) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `test` (`identifiant`) values
('99945I'), ('99951I'), ('99953I'), ('99961I'), ('99963I'), ('999I'), ('I1003'), ('I1005'), ('I1012'), ('I1014'), ('I1015'), ('I1023'), ('I1041')
--------------
--------------
select * from test
--------------
+----+-------------+
| id | identifiant |
+----+-------------+
| 1 | 99945I |
| 2 | 99951I |
| 3 | 99953I |
| 4 | 99961I |
| 5 | 99963I |
| 6 | 999I |
| 7 | I1003 |
| 8 | I1005 |
| 9 | I1012 |
| 10 | I1014 |
| 11 | I1015 |
| 12 | I1023 |
| 13 | I1041 |
+----+-------------+
--------------
select id, right(identifiant, 1) as lettre, left(identifiant, length(identifiant) - 1) as chiffre
from test having lettre REGEXP '^[a-z,A-Z]$'
--------------
+----+--------+---------+
| id | lettre | chiffre |
+----+--------+---------+
| 1 | I | 99945 |
| 2 | I | 99951 |
| 3 | I | 99953 |
| 4 | I | 99961 |
| 5 | I | 99963 |
| 6 | I | 999 |
+----+--------+---------+
--------------
alter table `test` add column lettre char(01) NOT NULL after identifiant
--------------
--------------
alter table `test` add column chiffre integer unsigned NOT NULL after lettre
--------------
--------------
update test set lettre = case when identifiant regexp '^[0-9].*$'
then right(identifiant, 1)
else left(identifiant, 1)
end,
chiffre = case when identifiant regexp '^[0-9].*$'
then cast( left(identifiant, length(identifiant) - 1) as unsigned)
else cast(right(identifiant, length(identifiant) - 1) as unsigned)
end
--------------
--------------
update test set identifiant = concat(right(identifiant, 1), left(identifiant, length(identifiant) - 1))
where identifiant regexp '^[0-9].*$'
--------------
--------------
select * from test
--------------
+----+-------------+--------+---------+
| id | identifiant | lettre | chiffre |
+----+-------------+--------+---------+
| 1 | I99945 | I | 99945 |
| 2 | I99951 | I | 99951 |
| 3 | I99953 | I | 99953 |
| 4 | I99961 | I | 99961 |
| 5 | I99963 | I | 99963 |
| 6 | I999 | I | 999 |
| 7 | I1003 | I | 1003 |
| 8 | I1005 | I | 1005 |
| 9 | I1012 | I | 1012 |
| 10 | I1014 | I | 1014 |
| 11 | I1015 | I | 1015 |
| 12 | I1023 | I | 1023 |
| 13 | I1041 | I | 1041 |
+----+-------------+--------+---------+
--------------
describe test
--------------
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| identifiant | varchar(255) | NO | | NULL | |
| lettre | char(1) | NO | | NULL | |
| chiffre | int(10) unsigned | NO | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager