简体   繁体   English

SQL搜索和替换仅在一个表的一个列中

[英]SQL Search & Replace in only ONE Column on ONE Table

I have been building a Magento site and the SKU list got a little erratic with automation. 我一直在构建Magento网站,并且SKU列表在自动化方面有些不稳定。 I am currently going through phpMyAdmin and removing " YRS " and " MO " off the end of SKUs instead of opening every product in the admin and saving it through there. 我目前正在通过phpMyAdmin并从SKU的末尾删除“ YRS ”和“ MO ”,而不是在管理员中打开每个产品并保存在那里。

I would like to find a way to automate this. 我想找到一种自动化的方法。 I need to search only one column from one table and replace/erase the " YRS " and " MO " effectively turning " 10-2354-03/06 MO " into " 10-2354-03/06 " 我只需要从一张表中搜索一列,然后替换/擦除“ YRS ”和“ MO ”,即可将“ 10-2354-03 / 06 MO ”有效地转换为“ 10-2354-03 / 06

I'm thinking something like "search > catalog_product_entity * sku |for| YRS & MO | replace ||" 我在想类似“搜索> catalog_product_entity * sku | for | YRS MO |替换||”之类的东西 (i don't know SQL queries so forgive me for such hacked coding) (我不知道SQL查询,所以请原谅我这种被黑的编码)

Capturing the preceding space isn't completely necessary but would be helpful for later on incase something needs to be added on. 捕获前面的空间并不是完全必要的,但是在以后需要添加某些内容的情况下将很有用。 There are other variables so something that has a "replace XXX with YYY" would be awesome :) 还有其他变量,因此具有“用YYYY替换XXX”的功能会很棒:)

Are there any simple queries I can run to make this happen? 我可以运行任何简单的查询来做到这一点吗?

Table is "catalog_product_entity" and column is "sku" 表是“ catalog_product_entity”,列是“ sku”

This update statement should do it: 此更新语句应执行以下操作:

update catalog_product_entity
   set sku = trim(trailing ' YRS' from trim(trailing ' MO' from sku))

If you want to test it in a select statement, run: 如果要在select语句中对其进行测试,请运行:

select trim(trailing ' YRS' from trim(trailing ' MO' from sku))
from   catalog_product_entity

See sql fiddle test: http://sqlfiddle.com/#!2/699ce/2/0 请参阅sql小提琴测试: http ://sqlfiddle.com/#!2 / 699ce / 2/0

(removes ' MO' or ' YRS' from the end of the string) (从字符串末尾删除“ MO”或“ YRS”)

Steven, 史蒂文

If this is MSSQL Server, you should be able to use something like the following: 如果这是MSSQL Server,则应该可以使用如下所示的内容:

update YOUR_TABLE_NAME 更新YOUR_TABLE_NAME

set catalog_product_entity = replace( catalog_product_entity, ' MO', '' ) 设置catalog_product_entity = replace(catalog_product_entity,'MO','')

from YOUR_TABLE_NAME 来自YOUR_TABLE_NAME

Before you run an update statement, try running just a select to compare the original values with your new values. 在运行更新语句之前,请尝试仅运行一次select来将原始值与新值进行比较。 Something like this: 像这样:

select catalog_product_entity , replace( catalog_product_entity , ' MO', '' ) from YOUR_TABLE_NAME 从YOUR_TABLE_NAME中选择catalog_product_entity,替换(catalog_product_entity,'MO','')

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

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