简体   繁体   English

如何在 mysql 列中查找和替换单词?

[英]how to find and replace a word in a mysql column?

I have a column containing list of streets.我有一列包含街道列表。 I need to replace 'street' with 'St'.我需要用“St”替换“street”。 The replacement can be made in the current column or in a new column with the address in the required format.可以在当前列或具有所需格式的地址的新列中进行替换。 The following is the sample data.以下是样本数据。 'Column 1' contains the data in current format. “第 1 列”包含当前格式的数据。 'Column 2' contains the data in the desired format. “第 2 列”包含所需格式的数据。

Column 1         Column 2
Hillary Street   Hillary St
Golf Road        Golf Road
Oldwood Street   Oldwood St

How do I do this?我该怎么做呢?

Edit:编辑:

This query works for this:此查询适用于此:

UPDATE table SET column = REPLACE(column, 'Street', 'St');

Is it possible to set a rule to this column.是否可以为此列设置规则。 Such that all data added to this get formatted this way automatically?这样添加到此的所有数据都会自动以这种方式格式化? Or I need to repeat this query each time?或者我每次都需要重复这个查询?

Run a query like this to update in the same column:运行这样的查询以在同一列中更新:

UPDATE table 
   SET column = REPLACE(column, 'Street', 'St');

So, if i understand correctly, you want to modify the data on the database based on wether or not you're using the word street.因此,如果我理解正确,您想根据您是否使用街道一词来修改数据库中的数据。 I think this is the call u need.我认为这是你需要的电话。

update [table_name] set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]');

I think this is the method you need to use, so your ending code will look something like我认为这是您需要使用的方法,因此您的结束代码看起来像

update myTable set address = replace(address,'street','St');

Is this what you meant?这是你的意思吗?

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

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