为了对列值使用LPAD()
或RPAD()
函数,我们需要将列名指定为这些函数的第一个参数。遵循“学生”表中的示例将使其更加清晰-
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)