
- 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
SET only two values for all the rows in a MySQL table based on conditions?
For condition based update, use UPDATE and IF(). Let us first create a table −
mysql> create table DemoTable ( Value int ); Query OK, 0 rows affected (0.47 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 10 | | 20 | | 10 | | 20 | | 30 | +-------+ 5 rows in set (0.00 sec)
Following is the query to update based on a condition −
mysql> update DemoTable set Value=if(Value=10,20,10); Query OK, 5 rows affected (0.20 sec) Rows matched: 5 Changed: 5 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 20 | | 10 | | 20 | | 10 | | 10 | +-------+ 5 rows in set (0.00 sec)
- Related Articles
- Delete only some rows from a table based on a condition in MySQL
- Set value only for NULL values in a MySQL table
- How can I export values based on some conditions from MySQL table into a file?
- Select all duplicate MySQL rows based on one or two columns?
- Select values that meet different conditions on different rows in MySQL?
- Replace records based on conditions in MySQL?
- How to select only 3 ordered rows on a MySQL table?
- How to replace rows in a MySQL table with conditions?
- Count values based on conditions and display the result in different columns with MySQL?
- Display only the default values set for columns in MySQL
- Set conditions for columns with values 0 or 1 in MySQL
- How to create a MySQL table based on JDBC Result Set?
- Update quantity in MongoDB based on two conditions?
- Delete only specific rows in a table with MySQL
- MySQL query with two boolean conditions to extract date based on hour?

Advertisements