mySQL table update
I need a SQL statement to update my data this is the lowdown
I have emails in my dbase that are typos ex: user@netscap.com - should be 'user@netscape.com' user@ail.com - should be 'user@aol.com Now these email addresses are in a table called lm_users and all i need to do is replace the 'ail' part with 'aol' but retain the other part of the address, obviously. Now i know how to do a replace for each one that is bad, but i need a general SQL statment that will look for all 'ail' addresses in this table and make the appropriate change without affecting the rest of the address. Sorta like a find-replace in word. I had thought about a dump file then using just taht a find and replace, just wanted to see if the gurus could save me from having to import and export etc. Ehhh help Zack |
You may want to take a look at the string functions here:
http://www.mysql.com/doc/en/String_functions.html In particular to this one: REPLACE(str,from_str,to_str) Returns the string str with all occurrences of the string from_str replaced by the string to_str: mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww'); -> 'WwWwWw.mysql.com' ...and that should work for you. -Josh |
OK i see but what about...
What about this here is theproblem...
I would still need to enter a string right? ex I have multiple users with typos for netscap.net should be netscape.net Would that still work? Zack |
You're going to have to perform a SELECT query to obtain those strings... Play around with that, and when I get home tonight I'll whip something up.
-Josh |
GREAT yeah had some probs
OK great yeah had some probs with the thing not very good at this i guess
I would VERY MUCH appreciate a whip up!! Zack |
DropHit,
Ok, sorry about the delay, I had a tour of an internet exchange tonight, very boring stuff! All right, just to make sure what I have done works, I have created a test database on my own system here and here is what I have come up with: Code:
mysql> update <tablename> You are updating a table, so you use update <insert name of table here>, then you want to tell it what to update, we will use SET <insert column/field name here> and you want to tell it what to set it to. We will use the replace function, which allows us to enter the fieldname, then the string to get replace within that field, then the string to replace it with. Bear in mind that the user portion of the field will not change, only the portion that matches the middle field of the replace function. I hope this works for you, if it doesnt, post again and I'll get back to you tomorrow. -Josh Code:
Sample output: |
All times are GMT -5. The time now is 10:02 PM. |