Home  >  Article  >  Database  >  How to update a MySQL table after filling a string with a column's value?

How to update a MySQL table after filling a string with a column's value?

WBOY
WBOYforward
2023-09-22 16:17:041019browse

用列的值填充字符串后如何更新 MySQL 表?

We can use the LPAD() or RPAD() function with the UPDATE clause to update the value of a column in the MySQL table while filling the string. Below is an example using 'examination_btech' table which will make it clearer -

Example

Suppose we want to append the value of string '(CSE)' at the end of column course and If you also want to update the table, you can do it using the following query -

mysql> Update examination_btech set course = RPAD(Course, 11,'(CSE)');
Query OK, 10 rows affected (0.16 sec)

mysql> Select * from examination_btech;
+-----------+----------+-------------+
| RollNo    | Name     | Course      |
+-----------+----------+-------------+
| 201712001 | Rahul    | B.tech(CSE) |
| 201712002 | Raman    | B.tech(CSE) |
| 201712003 | Sahil    | B.tech(CSE) |
| 201712004 | Shalini  | B.tech(CSE) |
| 201712005 | Pankaj   | B.tech(CSE) |
| 201712006 | Mohan    | B.tech(CSE) |
| 201712007 | Yash     | B.tech(CSE) |
| 201712008 | digvijay | B.tech(CSE) |
| 201712009 | Gurdas   | B.tech(CSE) |
| 201712010 | Preeti   | B.tech(CSE) |
+-----------+----------+-------------+
10 rows in set (0.00 sec)

From the above result set, it is obvious that "(CSE)" has finally been populated with the value of the "course" column and the table is also updated.

Similarly, with the help of the LPAD() function, we can fill the string at the beginning and update the table.

The above is the detailed content of How to update a MySQL table after filling a string with a column's value?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete