乐闻世界logo
搜索文章和话题

How to prepend a string to a column value in MySQL?

1个答案

1

In MySQL, you can use the CONCAT() function to prepend a string to column values. The CONCAT() function combines multiple string values into a single string. To prepend a specific string to all values in a column, use the following SQL statement:

sql
UPDATE table_name SET column_name = CONCAT('string_to_add', column_name) WHERE condition;

Here is a specific example:

Consider a table named employees with a column called name. To prepend 'Mr. ' to all employees' names, write the following SQL statement:

sql
UPDATE employees SET name = CONCAT('Mr. ', name);

This command prepends 'Mr. ' to all employees' names in the employees table.

If you only want to update specific records, specify conditions in the WHERE clause. For example, to prepend 'Mr. ' to employees whose names start with 'J':

sql
UPDATE employees SET name = CONCAT('Mr. ', name) WHERE name LIKE 'J%';

This command finds all employees with names starting with 'J' and prepends 'Mr. ' to their names.

2024年8月7日 00:30 回复

你的答案