为了将 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)