
- 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
Update multiple rows in a single column in MySQL?
To update multiple rows in a single column, use CASE statement. Let us first create a table −
mysql> create table updateMultipleRowsDemo -> ( -> StudentId int, -> StudentMathScore int -> ); Query OK, 0 rows affected (0.63 sec)
Following is the query to insert records in the table using insert command −
mysql> insert into updateMultipleRowsDemo values(10001,67); Query OK, 1 row affected (0.14 sec) mysql> insert into updateMultipleRowsDemo values(10002,69); Query OK, 1 row affected (0.15 sec) mysql> insert into updateMultipleRowsDemo values(10003,89); Query OK, 1 row affected (0.14 sec) mysql> insert into updateMultipleRowsDemo values(10004,99); Query OK, 1 row affected (0.13 sec) mysql> insert into updateMultipleRowsDemo values(10005,92); Query OK, 1 row affected (0.13 sec)
Following is the query to display all records from the table using select statement −
mysql> select * from updateMultipleRowsDemo;
This will produce the following output −
+-----------+------------------+ | StudentId | StudentMathScore | +-----------+------------------+ | 10001 | 67 | | 10002 | 69 | | 10003 | 89 | | 10004 | 99 | | 10005 | 92 | +-----------+------------------+ 5 rows in set (0.00 sec)
Here is the query to update multiple rows in a single column in MySQL −
mysql> UPDATE updateMultipleRowsDemo -> SET StudentMathScore= CASE StudentId -> WHEN 10001 THEN 45 -> WHEN 10002 THEN 52 -> WHEN 10003 THEN 67 -> END -> WHERE StudentId BETWEEN 10001 AND 10003; Query OK, 3 rows affected (0.19 sec) Rows matched: 3 Changed: 3 Warnings: 0
Let us check the value is updated or not −
mysql> select * from updateMultipleRowsDemo;
This will produce the following output
+-----------+------------------+ | StudentId | StudentMathScore | +-----------+------------------+ | 10001 | 45 | | 10002 | 52 | | 10003 | 67 | | 10004 | 99 | | 10005 | 92 | +-----------+------------------+ 5 rows in set (0.00 sec)
- Related Articles
- Update multiple rows in a single MongoDB query?
- How to update multiple rows using single WHERE clause in MySQL?
- How can we update columns values on multiple rows with a single MySQL UPDATE statement?
- MySQL update multiple records in a single query?
- Insert multiple rows in a single MySQL query
- Update only a single column value in MySQL
- Multiple Inserts for a single column in MySQL?
- Update multiple columns of a single row MySQL?
- How to get multiple rows in a single MySQL query?
- How to obtain multiple rows in a single MySQL query?
- Best way to update a single column in a MySQL table?
- Concatenate multiple rows and columns in a single row with MySQL
- Select multiple columns and display in a single column in MySQL?
- How to update multiple rows and left pad values in MySQL?
- How to insert multiple rows with single MySQL query?

Advertisements