Find and replace a part of URL records in MySQL?

MySQLMySQLi Database

The simplest way to replace records is using MySQL REPLACE() −

mysql> create table DemoTable2025
   -> (
   -> URL text
   -> );
Query OK, 0 rows affected (0.88 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable2025 values('http=//www.facebook.com');
Query OK, 1 row affected (0.23 sec)

mysql> insert into DemoTable2025 values('http=//www.google.com');
Query OK, 1 row affected (0.15 sec)

mysql> insert into DemoTable2025 values('http=//www.gmail.com');
Query OK, 1 row affected (0.26 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable2025;

This will produce the following output −

+-------------------------+
| URL                     |
+-------------------------+
| http=//www.facebook.com |
| http=//www.google.com   |
| http=//www.gmail.com    |
+-------------------------+
3 rows in set (0.00 sec)

Here is the query to find and replace a part of URL records −

mysql> update DemoTable2025
   -> set URL=replace(URL,'http=','https:');
Query OK, 3 rows affected (0.21 sec)
Rows matched: 3 Changed: 3 Warnings: 0

Let us check the table records once again −

mysql> select *from DemoTable2025;

This will produce the following output −

+--------------------------+
| URL                      |
+--------------------------+
| https://www.facebook.com |
| https://www.google.com   |
| https://www.gmail.com    |
+--------------------------+
3 rows in set (0.00 sec)
raja
Published on 06-Apr-2020 13:30:44
Advertisements