MySQL REPLACE statement with dynamic variables
I just wrote a little mysql snippet that will replace all instances of ‘domain1′ to ‘domain2′ in a given table and column. So why not share it. All you need to do is set the variable values in first 4 lines of code below:
SET @tblname = "tablename"; SET @colname = "columnname"; SET @lookfor = "'domain1'"; SET @replacewith = "'domain2'"; SET @qry = CONCAT('UPDATE ',@tblname, ' SET ', @colname, ' = REPLACE(' ,@colname, ',', @lookfor, ',',@replacewith,')'); PREPARE QUERY FROM @qry; EXECUTE QUERY;
Obviously, you can use this technique to construct any MySQL query.
Cheers
Marko
Related posts
No comments yet.


Leave a comment