
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL search and replace record from a list of records
Let us first create a table −
mysql> create table DemoTable -> ( -> ListOfName text -> ); Query OK, 0 rows affected (0.66 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Carol,Sam,John,David,Bob,Mike,Robert,John,Chris,James,Jace'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------------------------------------------------------------+ | ListOfName | +------------------------------------------------------------+ | Carol,Sam,John,David,Bob,Mike,Robert,John,Chris,James,Jace | +------------------------------------------------------------+ 1 row in set (0.00 sec)
Here is the query to search and replace a record from a list of records−
mysql> update DemoTable -> set ListOfName= -> concat(substring_index(ListOfName,'John',2) ,'Adam', SUBSTRING_INDEX(ListOfName, 'John', -1)); Query OK, 1 row affected (0.37 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+------------------------------------------------------------+ | ListOfName | +------------------------------------------------------------+ | Carol,Sam,John,David,Bob,Mike,Robert,Adam,Chris,James,Jace | +------------------------------------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Perform search/replace for only the first occurrence of a character in MySQL table records?
- Find and replace a part of URL records in MySQL?
- Exclude some ID records from a list and display rest in MySQL
- MySQL query to search record with apostrophe?
- Find out the popular domains from a MySQL table with domain records and search volume
- Replace a specific duplicate record with a new value in MySQL
- Fastest way to search for a date from the date records in MySQL
- Replace records based on conditions in MySQL?
- Order date records and fetch the 2nd ordered record in MySQL
- How to search a record by date in MySQL table?
- How to find a specific record from a list of values with semicolon in MySQL?
- MySQL query to retrieve records from the part of a comma-separated list?
- How to remove Duplicate Records except a single record in MySQL?
- How can we search a record from MySQL table having a date as a value in it?
- Search records on the basis of date in MySQL?

Advertisements