
- 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 update multiple records in a single query?
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Marks1 int, -> Marks2 int, -> Marks3 int -> ); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Marks1,Marks2,Marks3) values(45,67,34); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable(Marks1,Marks2,Marks3) values(89,87,56); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Marks1,Marks2,Marks3) values(87,56,54); Query OK, 1 row affected (0.31 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+--------+--------+--------+ | Id | Marks1 | Marks2 | Marks3 | +----+--------+--------+--------+ | 1 | 45 | 67 | 34 | | 2 | 89 | 87 | 56 | | 3 | 87 | 56 | 54 | +----+--------+--------+--------+ 3 rows in set (0.00 sec)
Here is the query to update multiple records −
mysql> update DemoTable -> set Marks1=98,Marks2=96,Marks3=99 -> where Id=1; Query OK, 1 row affected (0.18 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 −
+----+--------+--------+--------+ | Id | Marks1 | Marks2 | Marks3 | +----+--------+--------+--------+ | 1 | 98 | 96 | 99 | | 2 | 89 | 87 | 56 | | 3 | 87 | 56 | 54 | +----+--------+--------+--------+ 3 rows in set (0.00 sec)
- Related Articles
- Update multiple rows in a single MongoDB query?
- Is there any easy way to add multiple records in a single MySQL query?
- Update multiple rows in a single column in MySQL?
- A single MySQL query to update only specific records in a range without updating the entire column
- Update multiple columns of a single row MySQL?
- Multiple COUNT() for multiple conditions in a single MySQL query?
- Update two columns with a single MySQL query
- Implement multiple COUNT() in a single MySQL query
- Change multiple columns in a single MySQL query?
- Insert multiple rows in a single MySQL query
- MySQL query to sort multiple columns together in a single query
- Implement multiple LIKE operators in a single MySQL query
- MySQL query to insert multiple records quickly
- Delete records from a MySQL table with IN() in a single query
- How to bulk update MySQL data with a single query?

Advertisements