在MySQL中,如何在邮政编码前面加上“ 0”?
要使用0填充邮政编码,请使用LPAD()MySQL中的function。语法如下-
SELECT LPAD(yourColumnName, columnWidth+1, '0') as anyVariableName from yourTableName;
要了解上述LPAD()将前垫邮政编码添加0的概念,让我们创建一个表。该表的列之一是邮政编码。以下是创建表的查询。
mysql> create table ZipCodePadWithZeroDemo −> ( −> Name varchar(200), −> YourZipCode int(6) −> );
在表中插入一些记录。插入记录的查询如下-
mysql> insert into ZipCodePadWithZeroDemo values('John',23455);
mysql> insert into ZipCodePadWithZeroDemo values('Carol',46523);
mysql> insert into ZipCodePadWithZeroDemo values('Johnson',12345);
mysql> insert into ZipCodePadWithZeroDemo values('David',34567);显示表中的所有记录。查询如下-
mysql> select *from ZipCodePadWithZeroDemo;
以下是输出-
+---------+-------------+ | Name | YourZipCode | +---------+-------------+ | John | 23455 | | Carol | 46523 | | Johnson | 12345 | | David | 34567 | +---------+-------------+ 4 rows in set (0.00 sec)
实施我们在开头讨论的语法,以添加值为0的前面板邮政编码。查询如下-
mysql> SELECT LPAD(YourZipCode, 6, '0') as UPDATEDZIPCODE from ZipCodePadWithZeroDemo;
我们还将在这里获得名称-
mysql> SELECT Name,LPAD(YourZipCode, 6, '0') as UPDATEDZIPCODE from ZipCodePadWithZeroDemo;
以下输出显示名称以及邮政编码-
+---------+----------------+ | Name | UPDATEDZIPCODE | +---------+----------------+ | John | 023455 | | Carol | 046523 | | Johnson | 012345 | | David | 034567 | +---------+----------------+ 4 rows in set (0.00 sec)
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬