简体   繁体   English

更改表将文本添加到非空值

[英]Alter table add text to non-empty value

Can someone give me an example of how I can alter a table value that has text inside already, and keep that text but add new text in front of the existing text?有人可以给我一个示例,说明如何更改已包含文本的表值,并保留该文本但在现有文本前添加新文本? I'm trying to add additional text to 1600+ fields.我正在尝试向 1600 多个字段添加其他文本。

I was looking at MySQL and found reference to concatenate CONCAT , but as I understand, it adds text after a certain value.我正在查看 MySQL 并找到对 concatenate CONCAT的引用,但据我所知,它会在某个值之后添加文本。

If you want to change the value of a column use the UPDATE statement:如果要更改列的值,请使用 UPDATE 语句:

update myTable set myColumn = concat('some text before', myColumn, 'some text after') where <where condition>

You can add text where you want.您可以在需要的位置添加文本。 Just concatenate them in the way you like.只需以您喜欢的方式连接它们即可。

select *,concat('your_text ',field) as new_text from table 

or或者

select *,concat(field,' your_text') as new_text from table 

It exists also concat_ws() function where ws means "with separator".它也存在 concat_ws() function ,其中 ws 表示“带分隔符”。

select *,concat_ws(' ','your_text',field) as new_text from table.

Once you're sure you've the right new content convert the select in an update query.一旦您确定您拥有正确的新内容,请在更新查询中转换 select。 It's always a good practice trying the equivalent select before doing some mistake with update queries.在更新查询出错之前尝试等效的 select 始终是一个好习惯。

update table set field = concat('new text ',field)
where field is not null or field <> ""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM