
- 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
Change multiple columns in a single MySQL query?
For this, use UPDATE and REPLACE() in MySQL. Let us first create a table −
mysql> create table DemoTable -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(100), -> StudentCountryName varchar(100) -> ); Query OK, 0 rows affected (0.67 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentName,StudentCountryName) values('John','US'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql select *from DemoTable;
Output
This will produce the following output −
+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ | 1 | John | US | +-----------+-------------+--------------------+ 1 row in set (0.00 sec)
Following is the query to update multiple columns −
mysql> update DemoTable -> set StudentName=replace(StudentName,'John','Chris'), -> StudentCountryName=replace(StudentCountryName,'US','UK'); Query OK, 1 row affected (0.69 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
Output
This will produce the following output −
+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ | 1 | Chris | UK | +-----------+-------------+--------------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to sort multiple columns together in a single query
- How to alter column type of multiple columns in a single MySQL query?
- How can we sort multiple columns in a single query?
- Multiple COUNT() for multiple conditions in a single MySQL query?
- Implement multiple COUNT() in a single MySQL query
- MySQL update multiple records in a single query?
- Insert multiple rows in a single MySQL query
- Update multiple columns of a single row MySQL?
- Update two columns with a single MySQL query
- How to sort multiple columns with a single query?\n
- Implement multiple LIKE operators in a single MySQL query
- Count two different columns in a single query in MySQL?
- MySQL query to combine two columns in a single column?
- How to get multiple rows in a single MySQL query?
- How to obtain multiple rows in a single MySQL query?

Advertisements