开发者

how to replace an element in every column of database table?

I am not a guru of databases, do most of the things through PHP, like data manipulation as well, even if it is an internal task. However, PHP is not being very useful in this case. I have a table with about 0.4 million records in it and it has like 16 columns, everything else works fine but I populated this table from a text file and that text file had most of the values in each column in double quotes such as "United States of America" "London" etc.

I want to remove those double quotes where ever I 开发者_运维技巧can find them through a single query or SQL script. I have tried PHP and its str_replace method but apparently the records are far too huge for it to continue applying this function on each record and then run 2 queries for each value. Can there be any SQL solution for it?

The database is MySQL, engine can be switched from MyISAM to InnoDB. I have seen this solution here: SQL: search for a string in every varchar column in a database but not sure if it will work for me. Thanks.


(untested)

update mytable set mycol = case
    when mycol like '"%"' then trim(both '"' from mycol)
    else mycol end;

Actually, you probably don't even need the case expression:

update mytable set mycol = trim(both '"' from mycol)'

Repeat the query for other columns, or extend this one to cover all of your columns.


REPLACE() should work fine.

UPDATE `table` SET `column` = REPLACE(`column`, '"', '')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜