要添加前导零,可以使用LPAD()
。让我们首先创建一个表-
mysql> create table DemoTable ( Code varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('JS'); mysql> insert into DemoTable values('CB'); mysql> insert into DemoTable values('DM'); mysql> insert into DemoTable values('CT');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+------+ | Code | +------+ | JS | | CB | | DM | | CT | +------+ 4 rows in set (0.00 sec)
现在,让我们在MySQL中添加前导零-
mysql> update DemoTable set Code=LPAD(Code, 8, '0'); Rows matched: 4 Changed: 4 Warnings: 0
让我们再次检查表记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----------+ | Code | +----------+ | 000000JS | | 000000CB | | 000000DM | | 000000CT | +----------+ 4 rows in set (0.00 sec)