简体   繁体   English

MySQL - 替换列中的字符

[英]MySQL - Replace Character in Columns

Being a self-taught newbie, I created a large problem for myself. 作为一个自学成才的新手,我为自己创造了一个大问题。 Before inserting data in to my database, I've been converting apostrophes (') in a string, to double quotes (""), instead of the required back-slash and apostrophe (\\'), which MySQL actually requires. 在将数据插入我的数据库之前,我一直在将字符串中的撇号(')转换为双引号(“”),而不是MySQL实际需要的反斜杠和撇号(\\')。

Before my table grows more than the 200,000 rows it already is, I thought it was best to rectify this issue immediately. 在我的桌子增长超过20万行之前,我认为最好立即纠正这个问题。 So I did some research and found the SQL REPLACE function, which is great, but I'm now confused. 所以我做了一些研究并找到了SQL REPLACE函数,这很棒,但我现在很困惑。

In ASP, I was doing this: 在ASP中,我这样做:

str = Replace(str,"'","""")

If I look at my database in SQL Workbench, the symbol I converted is now a single quote ("), which has confused me a little. I understand why it changed from double to single, but I don't know which one I'm meant to be changing now. 如果我在SQL Workbench中查看我的数据库,我转换的符号现在是单引号(“),这让我有点困惑。我明白为什么它从双变为单,但我不知道哪一个我'我打算现在改变。

To go through and rectify my problem using SQL REPLACE, do I now convert single quotes (") to back-slash and apostrophes (\\') or do I convert double quotes ("") to back-slash and apostrophes (\\')? 要使用SQL REPLACE来解决我的问题,我现在将单引号(“)转换为反斜杠和撇号(\\'),还是将双引号(”“)转换为反斜杠和撇号(\\') ?

For example, this: 例如,这个:

SQL = " SELECT REPLACE(myColumn,"""","\'") FROM myTable "

or this: 或这个:

SQL = " SELECT REPLACE(myColumn,""","\'") FROM myTable "

I hope I explained myself well, any suggestions gratefully received as always. 我希望我能很好地解释自己,一如既往地感激地收到任何建议。 Any queries about my question, please comment. 有关我的问题的任何疑问,请评论。

Many thanks 非常感谢

-- UPDATE -- - 更新 -

I have tried the following queries but still fail to change the ( " ) in the data: 我尝试了以下查询,但仍无法更改数据中的(“):

SELECT REPLACE(caption,'\"','\'') FROM photos WHERE photoID = 3371
SELECT REPLACE(caption,'"','\'') FROM photos WHERE photoID = 3371
SELECT REPLACE(caption,'""','\'') FROM photos WHERE photoID = 3371

Yet if I search: 然而,如果我搜索:

SELECT COUNT(*) FROM photos WHERE caption LIKE '%"%'

I get 16,150 rows. 我得到16,150行。

-- UPDATE 2 -- - 更新2 -

Well, I have created a 'workaround'. 好吧,我已经创建了一个“解决方法”。 I managed to convert an entire column pretty quickly writing an ASP script, using this SQL: 我设法使用这个SQL很快地编写一个ASP脚本来转换整个列:

SELECT photoID, caption FROM photos WHERE caption LIKE '%""%';

and then in ASP I did: 然后在ASP中我做了:

caption = Replace(caption,"""","\'")

But I would still like to know why I couldn't achieve that with SQL? 但我仍然想知道为什么我无法用SQL实现这一目标?

Just running the SELECT statement will have no effect on the data. 只运行SELECT语句对数据没有影响。 You have to use an UPDATE statement with the REPLACE to make the change occur: 您必须使用带有REPLACEUPDATE语句来进行更改:

UPDATE photos
   SET caption = REPLACE(caption,'"','\'')

Here is a working sample: http://sqlize.com/7FjtEyeLAh 这是一个工作示例: http//sqlize.com/7FjtEyeLAh

Replace below characters 替换下面的字符

~ ! @ # $ % ^ & * ( ) _ +
` - = 
{ } |
[ ] \
: " 
; '

< > ?
, . 

with this SQL 用这个SQL

SELECT note as note_original, 

    REPLACE(
        REPLACE(
            REPLACE(
                REPLACE(
                    REPLACE(
                        REPLACE(
                            REPLACE(
                                REPLACE(
                                    REPLACE(
                                        REPLACE(
                                            REPLACE(
                                                REPLACE(
                                                    REPLACE(
                                                        REPLACE(
                                                            REPLACE(
                                                                REPLACE(
                                                                    REPLACE(
                                                                        REPLACE(
                                                                            REPLACE(
                                                                                REPLACE(
                                                                                    REPLACE(
                                                                                        REPLACE(
                                                                                            REPLACE(
                                                                                                REPLACE(
                                                                                                    REPLACE(
                                                                                                        REPLACE(
                                                                    REPLACE(
                                                                        REPLACE(
                                                                            REPLACE(
                                                                                REPLACE(
                                                                                    REPLACE(
                                                                                        REPLACE(
                                                                                            REPLACE(note, '\"', ''),
                                                                                        '.', ''),
                                                                                    '?', ''),
                                                                                '`', ''),
                                                                            '<', ''),
                                                                        '=', ''),
                                                                    '{', ''),
                                                                                                        '}', ''),
                                                                                                    '[', ''),
                                                                                                ']', ''),
                                                                                            '|', ''),
                                                                                        '\'', ''),
                                                                                    ':', ''),
                                                                                ';', ''),
                                                                            '~', ''),
                                                                        '!', ''),
                                                                    '@', ''),
                                                                '#', ''),
                                                            '$', ''),
                                                        '%', ''),
                                                    '^', ''),
                                                '&', ''),
                                            '*', ''),
                                        '_', ''),
                                    '+', ''),
                                ',', ''),
                            '/', ''),
                        '(', ''),
                    ')', ''),
                '-', ''),
            '>', ''),
        ' ', '-'),
    '--', '-') as note_changed FROM invheader

maybe I'd go by this. 也许我会这样做。

 SQL = SELECT REPLACE(myColumn, '""', '\'') FROM myTable

I used singlequotes because that's the one that registers string expressions in MySQL, or so I believe. 我使用单引号,因为那是在MySQL中注册字符串表达式的,或者我相信。

Hope that helps. 希望有所帮助。

如果你有“某事”并需要'某事',请使用replace(col, "\\"", "\\'")和反之。

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

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