首頁  >  文章  >  資料庫  >  如何在 MySQL 中將字串新增至列值之前?

如何在 MySQL 中將字串新增至列值之前?

Linda Hamilton
Linda Hamilton原創
2024-11-14 17:05:02844瀏覽

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);

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%';

以上是如何在 MySQL 中將字串新增至列值之前?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn