mysql Zerofill int 字段中的前导零在查询时不显示
我有一个带有自动增量零填充 ID 号的表。当我查询数据时,ID 丢失了前导零(即“000529”返回为“529”)。有没有办法保留前导零,甚至在查询语句中生成它们?我知道我可以使用 STRPAD 在 PHP 中生成它们,但对于我所在的特定项目,我想检索数据库中的数据。
I have a table with auto increment zerofill ID numbers. When I query the data the IDs lose their leading zeros (i.e. "000529" returns as "529"). Is there a way to preserve the leading zeros, or even generate them back in the query statement? I know I can generate them back in PHP using STRPAD, but for the specific project I am on I would like to retrieve the data as it is in the DB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用函数 LPAD() 显示用零填充的数字(左侧):
Use function LPAD() to show the numbers (left) padded with zeros:
这是 UNION ALL 部分将它们转换为 INT - 我还没有找到一个简单的解决方案。在我看来,这是一个 mySql 错误。
It's the UNION ALL part that is converting them to INT - I've yet to find a simple solution. To my mind it's a mySql bug.
当您选择带有前导零的字段并使用 UNION 时,它将删除前导零。
我通过添加找到了解决方法:
显然,
id_number
代表您尝试选择的任何零填充字段。When you select the field with leading zeros and use a UNION it will remove the leading zeros.
I found a workaround by adding:
Obviously,
id_number
represents whatever zero filled field you are trying to SELECT.