mysql1 Min Read

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

Gorav Singal

April 25, 2020

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')
Share

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…

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

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

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI video generation went from “cool demo” to “usable in production” in 2024-202…

AI Models in 2025 — Cost, Capabilities, and Which One to Use

AI Models in 2025 — Cost, Capabilities, and Which One to Use

Choosing the right AI model is one of the most impactful decisions you’ll make…

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

Generating one image with AI costs between $0.002 and $0.12. That might sound…

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

Two years ago, AI coding meant one thing: GitHub Copilot autocompleting your…

AI Agents Demystified — It's Just Automation With a Better Brain

AI Agents Demystified — It's Just Automation With a Better Brain

Let’s cut through the noise. If you read Twitter or LinkedIn, you’d think “AI…

Supply Chain Security — Protecting Your Software Pipeline

Supply Chain Security — Protecting Your Software Pipeline

In 2024, a single malicious contributor nearly compromised every Linux system on…