Forum Moderators: open
I run a message board forum (similar in function to webmasterworld.com) and need to update the text in all posts for a particular value, while leaving the rest of the text in tact.
For example, say I want to update every instance of my board name from 'webmaster world' to 'webmaster universe', without changing the other text. So a post that currently reads:
"Welcome to webmaster world where you'll find all your site design questions answered....Be sure to tell your friends about webmaster world"
becomes:
"Welcome to webmaster universe where you'll find all your site design questions answered....Be sure to tell your friends about webmaster universe"
Any help?
My table is named posts_table
The DB column is post_text
Something like:
SELECT REPLACE('webmaster world', 'world', 'universe');
UPDATE posts_table SET post_text...?;
(It's probably obvious that I don't know SQL)
UPDATE posts_table SET post_text = REPLACE (post_text, 'webmaster world', 'webmaster universe');
1) Is this a valid SQL query for MySQL (note, among other things, the wildcard '%' ending the LIKE statement):
UPDATE table_name SET col_name = REPLACE (col_name, 'old text', 'new text') WHERE post_text LIKE 'This is old text%';
2) If so, and my DB record now contains:
This is old text.
This is old text too.
This is my old text.
which will be the updated query result:
a:
This is new text.
This is new text too.
This is my old text.
or b:
This is new text.
This is new text too.
This is my new text.
(In other words, does WHERE return TRUE if the LIKE statement is found anywhere in the record - giving result b - or does it give a distinct result for each 'old text' it finds, giving result a?)
Can I use '%' as a wildcard in the REPLACE section?
No. It is going to be treated as a literal character, the percent sign.
Yes, that is a valid SQL statement (except that I'm guessing
col_namewould likely be the name of your column,
post_text).
And
a)being the result is indeed correct, that is exactly what would be updated. The text would be matched exactly until it reaches wildcard characters. So the comparison looks for the exact same sentence until it reaches the percent wildcard character and accepts anything after that.
Some helpful resources:
[dev.mysql.com...]
[dev.mysql.com...]
I've tried using escape characters (backslashes), then I saw in the MySql manual that you need to double backslashes in LIKE statements (which I'm using), but I still can't get it to work.
My latest attempt is:
SELECT * FROM db_table WHERE db_column LIKE '%\\\\\\"%';
The correct query should return positive for an entry like:
This is sample text with the string \" in it.
If it's easier, is it possible to designate a different escape character in the query, so as not to confuse it with the backslash I'm searching for? If so, what would that query be?