存储在 mysql 整数字段中时允许数字以 0 开头
我需要存储以 0 开头的电话号码,但每当我尝试将其存储在 MySql 表中时,开头的 0 就会被删除,因为实际上没有数字以 0 开头。
如何解决这个问题?我需要将字段类型从 Integer 更改为其他类型吗?
I need to store phone numbers starting with 0 but whenever i try to store this in MySql table the starting ZERO is removed because no number start with Zero actually.
How to solve this issue? Do I need to change the field type from Integer to another type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您还可以使用函数将所需的数字用前导零括起来。我创建了这个函数,如果“字符串”小于 2 位数字,则添加前导零(它用于向小时和分钟添加前导零)
如果你说,你想输出一个数字 2 作为 02,你会做 LeadZero(2);
如果数字长度小于 2 位,则只会添加零!例如leadZero(14);将返回 14
You can also wrap the number you want with a lead zero with a function. I made this function to add lead zero if the "string" is smaller than 2 digits (it was used to add lead zeroes to hours and minutes)
If you have say, a number 2 that you want to output as 02, you'd do leadZero(2);
This will only add a zero IF the number is less than 2 digits long ! For instance leadZero(14); will return 14
是的 - 数字字段仅存储数字值,而不存储这些值的格式(带有前导零的 paddin 是)。您应该
将字段类型从整数更改为 varchar 或 char(如果位数始终相同)。
将数字存储为整数,但根据需要在表示层中添加 0。
Yes - numeric fields only store the numeric values, not the formatting of those (which paddin with leading zeroes is). You should either
change the field type from integer to varchar or char (if # of digits is ALWAYS the same).
Store the number as integer BUT prepend 0 in your presentation layer as needed.
电话号码不是整数,您只会在尝试将它们存储为整数时遇到问题,而是将它们存储为字符串。
Phone numbers aren't integers and you will only end up with problems trying to store them as integers, store them as strings instead.
您可以使用 varchar 数据类型来解决此问题。
You can use data type as varchar to solve this.
电话号码也可以包含其他符号以提高可读性...电话号码的正则表达式类似于
[0-9+-()*#]+
。因此,您需要使用电话号码的文本字段以及一些验证。Phone numbers can contain other symbols for readability too... a regexp for a phone number looks something like
[0-9+-()*#]+
. So you need to use a text field for phone numbers, plus some validation.电话号码并不是真正的数字,因为它们不是序数。它们只是字符——事实上它们是数字是偶然的。
将它们存储在 varchar 中并继续:D
Phone numbers are not really numbers in the sense that they aren't ordinal. They're just characters - they fact that they are numbers is incidental.
Store them in a varchar and move on :D
将数据类型更改为 unsigned-zerofill 无论您使用什么,float、int、decimal(6,2)...只需将字段编辑为 unsigned-zerofill
change data type to unsigned-zerofill whatever you are using, float, int, decimal(6,2)... only edit the field to unsigned-zerofill