简体   繁体   中英

Query to REGEX_REPLACE in mysql as per another column

I have a table T with two columns C1 and C2.

I want to write a query as follows:

UPDATE TABLE T 
SET C2 = REGEX_REPLACE(
    "(REG_SUB_PART1)(REG_SUB_PART2)(REG_SUB_PART3)", 
    C1, 
    REG_SUB_PART1
) 
WHERE C2="ABC";

Effectively, I want to use another column C1, let's say URL " http://www.google.com " and set C2 to be a part of it, let's say "google.com" using $3 (third part) of regex "(http://)?(www\\.)?([a-zA-Z0-9]*)" .

As a result, C2 should be set as "google.com".

How could it be done using MySql?

PS: Please don't concentrate on specific regex.

There is no way to do regex capture groups in MySQL per http://dev.mysql.com/doc/refman/5.0/en/regexp.html . However, if you know the starting index in c1, an alternate way to accomplish a similar thing would be.

update t set c2= substring(c1,12) where c2 = 'abc';

This will get the substring 'google.com' from ' http://www.google.com ', provided that was the value in column c1.

Some other MySQL string functions that might come in handy are substring, substring_index and locate.

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