如何在 MySQL 中将列右对齐?
你可以使用 MySQL 的 LPAD() 函数。我们首先创建一个表:
mysql> create table DemoTable ( FullName varchar(100) ); Query OK, 0 rows affected (0.81 sec)
使用 insert 命令向表中插入记录:
mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('David Miller'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Sam Williams'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Carol Taylor'); Query OK, 1 row affected (0.47 sec)
使用 select 命令从表中显示记录:
mysql> select *from DemoTable;
这将产生以下结果:
+--------------+ | FullName | +--------------+ | John Smith | | David Miller | | Sam Williams | | Carol Taylor | +--------------+ 4 rows in set (0.00 sec)
以下是右对齐列的查询:
mysql> select lpad(FullName,70,' ') from DemoTable;
这将产生以下结果:
+------------------------------------------------------------------------+ | lpad(FullName,70,' ') | +------------------------------------------------------------------------+ | John Smith | | David Miller | | Sam Williams | | Carol Taylor | +------------------------------------------------------------------------+ 4 rows in set (0.00 sec)
广告