MySQL: update column by using regexp
Blogs20122012-11-23
1. MySQL, update column by using regexp
I want to update the values of a MySQL column, e.g. from:
http://www.example.com/news/ent//news/2002/09/02/ent-12.html?page=1223
to:
http://www.example.com/news/2002/09/02/ent-12.html?page=1223
For a huge table, if using:
- $ mysqldump -u user -p database table > dump.sql
- $ find /path/to/dump.sql -type f -exec sed -i ās/old_string/new_string/gā {} ;
- $ mysqlimport -u user -p database table < dump.sql
It would make processing complex and easy to occur errors. My solution is very simple: just using MySQLās substring(), concat(), they works very well: it takes around 10 seconds to process tens of thousands data.
update contents
set url=concat('http://www.example.com', substring(url,39))
where createdby='gossip.cgi'2. MySQL merge 2 tables
I have 2 MySql tables which have the same structure/indexes/unique index, each have tens of thousands data, and want to merge table2 into table1. The following is my way:
insert ignore into table1
select * from table2;I remember Oracle PL/SQL has the same syntax with MySQL for this kind of merge.
