如何将字符串添加到 MySQL 中的列值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/680801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to prepend a string to a column value in MySQL?
提问by santanu
I need a SQL update statement for updating a particular field of all the rows with a string "test" to be added in the front of the existing value.
我需要一个 SQL 更新语句来更新所有行的特定字段,并在现有值的前面添加一个字符串“test”。
For example, if the existing value is "try" it should become "testtry".
例如,如果现有值是“try”,它应该变成“testtry”。
回答by Paul Dixon
回答by Ferdinand Beyer
UPDATE tablename SET fieldname = CONCAT("test", fieldname) [WHERE ...]
回答by bvidinli
Many string update functions in MySQL seems to be working like this:
If one argument is null
, then concatenation or other functions return null
too.
So, to update a field with null
value, first set it to a non-null value, such as ''
MySQL 中的许多字符串更新函数似乎是这样工作的:如果一个参数是null
,则连接或其他函数null
也会返回。因此,要更新带有null
值的字段,首先将其设置为非空值,例如''
For example:
例如:
update table set field='' where field is null;
update table set field=concat(field,' append');
回答by soulmerge
That's a simple one
这是一个简单的
UPDATE YourTable SET YourColumn = CONCAT('prependedString', YourColumn);
回答by user3419778
- UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column1) where 1
- UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column2) where 1
- UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column2, 'newtring2') where 1
- UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column1) where 1
- UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column2) where 1
- UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column2, 'newtring2') where 1
We can concat same column or also other column of the table.
我们可以连接表的同一列或其他列。