Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2023-09-22 16:17:04
forward
1055 people have browsed it

用列的值填充字符串后如何更新 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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template