37

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).

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).

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.

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 ?

Thanks a ton.

4

4 回答 4

87

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

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

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

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

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

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

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

于 2012-11-13T13:25:23.710 回答
5

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

在测试查询之前,请先备份数据库。

于 2012-11-13T11:25:41.580 回答
5
update tablename set title = 'Mr. ' || title where ....
于 2012-11-13T10:23:41.383 回答
1

就这样做

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

UPDATE [masters].[country] SET Countrycode = '+' +Countrycode
于 2016-01-11T15:05:20.427 回答