Home > Database > Mysql Tutorial > How to Prepend a String to Column Values in MySQL?

How to Prepend a String to Column Values in MySQL?

Linda Hamilton
Release: 2024-11-14 17:05:02
Original
920 people have browsed it

How to Prepend a String to Column Values in MySQL?

Prepending a String to Column Values in MySQL

Problem:

You aim to modify a specific column in all rows of a MySQL table by adding a string ("test") to the beginning of the existing value. For instance, "try" would change to "testtry."

Solution:

Utilize the CONCAT function in an SQL update statement to achieve this:

UPDATE tbl SET col=CONCAT('test',col);
Copy after login

Advanced Update:

If you wish to selectively update only columns that do not already contain the "test" prefix, you can employ the following statement:

UPDATE tbl SET col=CONCAT('test',col)
WHERE col NOT LIKE 'test%';
Copy after login

The above is the detailed content of How to Prepend a String to Column Values in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template