Adding a String Prefix to Column Values in MySQL
To prepend a string to a column value in MySQL, you can employ the CONCAT function. This function combines multiple strings into a single one.
Syntax:
UPDATE table_name SET column_name = CONCAT('string_to_prepend', column_name);
Example:
To add the string "test" to the beginning of the "name" column in the "employees" table:
UPDATE employees SET name = CONCAT('test', name);
Example with Conditional Update:
If you only want to update rows where the current value of the column does not already have the string "test" prepended, you can use the following query:
UPDATE employees SET name = CONCAT('test', name) WHERE name NOT LIKE 'test%';
This ensures that "test" is only added to column values that don't contain it.
The above is the detailed content of How do I Add a String Prefix to Column Values in MySQL?. For more information, please follow other related articles on the PHP Chinese website!