简体   繁体   中英

Replace all occurences of string in column MYSQL database

I am working with a database with a column which consists of data of what I think is a GUID, c92e5daa-ae5b-4d8e-a20d-9c1f660a7081. I have 4 of these which occurs several times and I want to replace then with something more convenient like this:

c92e5daa-ae5b-4d8e-a20d-9c1f660a7081 --> String1
c92e5daa-ae5b-4d8e-a20d-9c1f660a7081 --> String2
c92e5daa-ae5b-4d8e-a20d-9c1f660a7081 --> String3
c92e5daa-ae5b-4d8e-a20d-9c1f660a7081 --> String4

Now I know that I have a few options. I could run a SQL query with the replace function, but that won't do it since the db is always getting new data. I've been thinking about replacing them with conditional statements in my program, but I'm not sure about that one either.

Any ideas of what would be the cleanest and best way of solving this?

IF you have currently-existing data in your database, I suspect the simplest solution is to run the update using a replace function, to modify your currently existing data, and then:

Perform your string validation and manipulation (in this case, checking its value and modifying it accordingly), in your application layer, to prevent ever inserting these strings in the database in the future. This has the advantage of simplicity.

You could, for instance, construct a dictionary type object and store the strings you want to replace as keys, with the replacement values as values. Then, when inserting, just check the dictionary for whether a key exists, if so, substitute its value.

UPDATE table SET column="String1" WHERE column="c92e5daa-ae5b-4d8e-a20d-9c1f660a7081"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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