~/mysql/mysql-update-query.md

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

1 min readApril 25, 2020by Gorav Singal#mysql
// 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')

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Fix Drupal MySQL Error: Got a Packet Bigger Than max_allowed_packet

Fix Drupal MySQL Error: Got a Packet Bigger Than max_allowed_packet

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…

Claude Code Skills — Build a Better Engineering Workflow

Claude Code Skills — Build a Better Engineering Workflow

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

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…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…