MySQL 用于在字段中的现有值前添加前缀的 SQL 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13358923/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:27:23  来源:igfitidea点击:

SQL query to prepend prefix to existing value in a field

mysqlsqljoomlaphpmyadmin

提问by Sean2015

I have searched and searched for an answer to this, and I think this must be child's play for anyone who KNOWS SQL (which is not me).

我已经搜索并搜索了这个问题的答案,我认为对于任何了解 SQL 的人(不是我)来说,这一定是儿戏。

I want to insert a prefix to the values in a field of a table in my DB.

我想在我的数据库中的表的字段中插入一个前缀。

More specifically, I have a table jos_content in which I have a field 'title' (which contains the titles of the content items of my joomla site).

更具体地说,我有一个表 jos_content,其中有一个字段“title”(其中包含我的 joomla 站点的内容项的标题)。

All the values in this field 'title' are names of individuals. Now all I want to do is add a prefix 'Mr.' to all the values of this field.

此字段“标题”中的所有值都是个人姓名。现在我想要做的就是添加一个前缀“先生”。到该字段的所有值。

I can do this from phpmyadmin by clicking the edit pencil icon and simply adding Mr. in front of all the values but I have about 750 rows and an SQL command which can insert a prefix of 'Mr.' in front of all values of this field will be a great help.

我可以在 phpmyadmin 中通过单击编辑铅笔图标并简单地在所有值前面添加 Mr. 来完成此操作,但我有大约 750 行和一个可以插入前缀“Mr.”的 SQL 命令。前面这个字段的所有值都会有很大帮助。

I have read about the 'UPDATE' commands but that REPLACES the value with what you provide. But I want to let the values remain and add a prefix before them.

我已经阅读了有关“更新”命令的内容,但是用您提供的内容替换了该值。但我想让这些值保留并在它们之前添加一个前缀。

Please can anyone help me achieve this with a SQL command ?

请任何人都可以用 SQL 命令帮助我实现这一目标吗?

Thanks a ton.

万分感谢。

回答by Jobin Jose

You have no other conditions like update this in all rows then you can try

您没有其他条件,例如在所有行中更新此内容,然后您可以尝试

UPDATE jos_content SET title = CONCAT('Mr. ', title) 

if you want to update conditionally that means particular row needs to update the you can use

如果你想有条件地更新这意味着特定的行需要更新你可以使用

 UPDATE jos_content SET title = CONCAT('Mr. ', title)  where fiedl_name ='condition'

eg: UPDATE jos_content SET title = CONCAT('Mr. ', title)  where id = '1'

this will update only one row which contain id=1.

这将只更新包含 id=1 的一行。

any way before doing this should keep a backup

在这样做之前的任何方式都应该保留备份

回答by sufleR

update tablename set title = 'Mr. ' || title where ....

回答by Valentin Despa

UPDATE jos_content SET title = CONCAT('Mr. ', title) WHERE 1

UPDATE jos_content SET title = CONCAT('Mr. ', title) WHERE 1

Before testing the query do make a database backup.

在测试查询之前做一个数据库备份。

回答by Thivan Mydeen

just do it like this

就这样做

If, for example, I want to add +symbol before the countrycode:

例如,如果我想在国家/地区代码之前添加 +symbol:

UPDATE [masters].[country] SET Countrycode = '+' +Countrycode