mysql|April 25, 2020|1 min read

MySql update query - Update column by string replacement in all records

TL;DR

UPDATE table SET col = REPLACE(col, 'old', 'new') WHERE col LIKE '%old%' — replaces a substring in all matching rows in one query.

MySql update query - Update column by string replacement in all records

Problem Statement

In a mysql table, I wanted to replace the hostname of the image URLs. And, it will be an update statement which will update all such records.

Query

update `TABLE_NAME` set COLUMN_NAME = REPLACE (COLUMN_NAME, 'STRING_TO_SEARCH', 
'STRING_TO_REPLACE_WITH') WHERE COLUMN_NAME like '%STRING_TO_SEARCH%';

In above query:

  • TABLE_NAME is thr name of your table
  • COLUMN_NAME is the name of your column which you want to update
  • STRING_TO_SEARCH is the string that you want to search and replace with something else
  • STRING_TO_REPLACE_WITH is the target string with which you want to replace the search string

Performance Optimization

Note that in above query, where clause is suppose to filter out the target set of strings on which you want to work upon.

  • First where clause will do its work to perform string match, and filters out the matched records. Note: it is running on all the records
  • Second, REPLACE will run on all the filtered records.

Now, if you notice that since REPLACE function will anyway has to do the search operation, what is the need of where clause. In reality, it is actually slowing down the system. Its not optimized. You can simply remove the where clause, and do something like:

update `TABLE_NAME` set COLUMN_NAME = REPLACE (COLUMN_NAME, 'STRING_TO_SEARCH', 
'STRING_TO_REPLACE_WITH')

Related Posts

How to Fix Drupal Mysql error - Communication link failure: 1153 Got a packet bigger than 'max_allowed_packet' bytes

How to Fix Drupal Mysql error - Communication link failure: 1153 Got a packet bigger than 'max_allowed_packet' bytes

Introduction While this topic may applicable to all mysql/mariadb users who…

How to connect to a running mysql service on host from a docker container on same host

How to connect to a running mysql service on host from a docker container on same host

Introduction I have a host running mysql (not on a container). I have to run an…

How to connect to mysql from nodejs, with ES6 promise

How to connect to mysql from nodejs, with ES6 promise

Introduction I had to develop a small automation to query some old mysql data…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

How to Be a Full-Time Freelancer: Resources, Finding Clients, and Building a Sustainable Business

How to Be a Full-Time Freelancer: Resources, Finding Clients, and Building a Sustainable Business

Making the leap from full-time employment to freelancing is one of the most…

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

Latest Posts

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…

HTTP Cookies Security — Everything Developers Get Wrong

HTTP Cookies Security — Everything Developers Get Wrong

Cookies are the single most important mechanism for web authentication. Every…

Format String Vulnerabilities — The Read-Write Primitive Hiding in printf()

Format String Vulnerabilities — The Read-Write Primitive Hiding in printf()

Format string vulnerabilities are unique in the exploit world. Most memory…

Buffer Overflow Attacks — How Memory Corruption Actually Works

Buffer Overflow Attacks — How Memory Corruption Actually Works

Buffer overflows are the oldest and most consequential vulnerability class in…