我正试图插入第一位数为零的数字
当我尝试添加前缀 000012345
时只会添加 12345
当我尝试添加前缀 012345
时只会添加 12345
我的数据类型是INT(11)
$sql = "INSERT INTO circle_call_prefixes (circle, prefix)
VALUES (?,?)";
$stmt = $dbh->prepare($sql);
$stmt ->execute(array($destination , $prefix ));
$Last_ID = $dbh->lastInsertId();
$sql_table2 = "INSERT INTO circle_call_destinations
(autoNo,destination, source_circle, comment) VALUES (?,?,?,?)";
$stmt = $dbh->prepare($sql_table2);
$stmt -> execute(array($Last_ID, $destination, $source_circle, $comment));
感谢
答案 0 :(得分:4)
在MySql中你可以填充字段,如果你知道最后的长度,因为你的列的长度是11,如果你需要在列的长度上填充0,那么你可以使用LPAD函数:
SELECT LPAD(12345, 11, '0') FROM tableName;
答案 1 :(得分:3)
如果你想要0作为前缀,则使用数据类型作为varchar(11)而不是int(11)。对于前导0的整数没有价值。
答案 2 :(得分:1)
由于Somail和Hanky建议将initial保持为0,所以应该将数据类型更改为varchar而不是int。
但是如果你想把你的数据类型保持为int,那么你可以采用以下方法 -
只需将您的数据类型更改为“MYCOL INT ZEROFILL”
如果您在此列中插入1,则它将存储为0000000001,如果您插入125,则为0000000125。
但是如果你坚持使用自己的数字只是012345那么就使用varchar。