如何使用 LPAD() 或 RPAD() 函数与 MySQL 表中一列的值一起使用?
若要将 LPAD() 或 RPAD() 函数与列值一起使用,我们需要将列名指定为这些函数的第一个参数。按照“Student”(学生)表的示例将使其更清晰 −
示例
mysql> Select Name, LPAD(Name,10,'*') from student; +---------+-------------------+ | Name | LPAD(Name,10,'*') | +---------+-------------------+ | Gaurav | ****Gaurav | | Aarav | *****Aarav | | Harshit | ***Harshit | | Gaurav | ****Gaurav | | Yashraj | ***Yashraj | +---------+-------------------+ 5 rows in set (0.08 sec) mysql> Select Name, RPAD(Name,10,'*') from student; +---------+-------------------+ | Name | RPAD(Name,10,'*') | +---------+-------------------+ | Gaurav | Gaurav**** | | Aarav | Aarav***** | | Harshit | Harshit*** | | Gaurav | Gaurav**** | | Yashraj | Yashraj*** | +---------+-------------------+ 5 rows in set (0.00 sec)
我们还可以在一个查询中将这两个函数用于列的值,如下所示 −
mysql> Select Name, RPAD(LPAD(Name,10,'* '),14,'* ') from student; +---------+----------------------------------+ | Name | RPAD(LPAD(Name,10,'* '),14,'* ') | +---------+----------------------------------+ | Gaurav | * * Gaurav* * | | Aarav | * * *Aarav* * | | Harshit | * *Harshit* * | | Gaurav | * * Gaurav* * | | Yashraj | * *Yashraj* * | +---------+----------------------------------+ 5 rows in set (0.00 sec)
广告