在MySQL表中选择并插入带有零的值
为此,您可以将INSERTINTOSELECT语句与一起使用LPAD()
。让我们首先创建一个表-
mysql> create table DemoTable1967 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserId varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1967(UserId) select LPAD(COALESCE(MAX(id), 0) + 1, 3, '0') from DemoTable1967; Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into DemoTable1967(UserId) select LPAD(COALESCE(MAX(id), 0) + 1, 3, '0') from DemoTable1967; Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into DemoTable1967(UserId) select LPAD(COALESCE(MAX(id), 0) + 1, 3, '0') from DemoTable1967; Records: 1 Duplicates: 0 Warnings: 0
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1967;
这将产生以下输出-
+----+--------+ | Id | UserId | +----+--------+ | 1 | 001 | | 2 | 002 | | 3 | 003 | +----+--------+ 3 rows in set (0.00 sec)