
- 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
How to replace rows in a MySQL table with conditions?
To set conditions and replace rows, use MySQL CASE statement. Let us first create a table −
mysql> create table DemoTable1481 -> ( -> PlayerScore int -> ); Query OK, 0 rows affected (0.42 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1481 values(454); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable1481 values(765); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1481 values(890); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1481;
This will produce the following output −
+-------------+ | PlayerScore | +-------------+ | 454 | | 765 | | 890 | +-------------+ 3 rows in set (0.00 sec)
Following is the query to replace rows in a MySQL table −
mysql> update DemoTable1481 -> set PlayerScore= case when PlayerScore=454 then 1256 -> when PlayerScore=765 then 1865 -> when PlayerScore=890 then 3990 -> end -> ; Query OK, 3 rows affected (0.17 sec) Rows matched: 3 Changed: 3 Warnings: 0
Let us check the table records once again −
mysql> select * from DemoTable1481;
This will produce the following output −
+-------------+ | PlayerScore | +-------------+ | 1256 | | 1865 | | 3990 | +-------------+ 3 rows in set (0.00 sec)
- Related Articles
- How to create conditions in a MySQL table with multiple columns?
- How to replace a character in a MySQL table?
- Replace records based on conditions in MySQL?
- SET only two values for all the rows in a MySQL table based on conditions?
- How to use REPLACE() function with column’s data of MySQL table?
- MySQL replace values in a table?
- Delete only specific rows in a table with MySQL
- How can MySQL find and replace the data with REPLACE() function to UPDATE the table?
- Fetch random rows from a table with MySQL
- How to select the last three rows of a table in ascending order with MySQL?
- Count rows having three or more rows with a certain value in a MySQL table
- Copy all rows of a table to another table in MySQL?
- How to optimize a MySQL table after deleting some rows?
- Get the number of rows in a particular table with MySQL
- Replace the empty values from a MySQL table with a specific value

Advertisements