
- 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 update all the entries except a single value in a particular column using MySQL?
To update all the entries while ignoring a single value, you need to use IF().
Let us first create a table −
mysql> create table DemoTable736 ( CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, CustomerName varchar(100), isMarried boolean ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable736(CustomerName,isMarried) values('Chris',0); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable736(CustomerName,isMarried) values('Robert',0); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable736(CustomerName,isMarried) values('David',0); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable736(CustomerName,isMarried) values('Mike',0); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable736(CustomerName,isMarried) values('Carol',1); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable736(CustomerName,isMarried) values('Bob',0); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable736;
This will produce the following output -
+------------+--------------+-----------+ | CustomerId | CustomerName | isMarried | +------------+--------------+-----------+ | 1 | Chris | 0 | | 2 | Robert | 0 | | 3 | David | 0 | | 4 | Mike | 0 | | 5 | Carol | 1 | | 6 | Bob | 0 | +------------+--------------+-----------+ 6 rows in set (0.00 sec)
Following is the query to update all the entries except a single value in a particular column using MySQL. Here, we are updating the “isMarried” column −
mysql> update DemoTable736 set isMarried=if(CustomerId=5,1,1); Query OK, 5 rows affected (0.13 sec) Rows matched: 6 Changed: 5 Warnings: 0
Let us check table records once again −
mysql> select *from DemoTable736;
This will produce the following output -
+------------+--------------+-----------+ | CustomerId | CustomerName | isMarried | +------------+--------------+-----------+ | 1 | Chris | 1 | | 2 | Robert | 1 | | 3 | David | 1 | | 4 | Mike | 1 | | 5 | Carol | 1 | | 6 | Bob | 1 | +------------+--------------+-----------+ 6 rows in set (0.00 sec)
- Related Articles
- Update only a single column value in MySQL
- How to increment all the rows of a particular column by 1 in a single MySQL query (ID column +1)?
- MySQL query to select all entries from a particular month
- Bulk change all entries for a particular field in MySQL?
- Order by desc except a single value in MySQL
- Set particular value of column without using update and while inserting values in MySQL
- Best way to update a single column in a MySQL table?
- Set all the columns of a MySQL table to a particular value with a single query
- Update multiple rows in a single column in MySQL?
- How to update a column with the same values (not all) using UPDATE, SET and LIMIT in MySQL?
- How to concatenate all values of a single column in MySQL?
- How to set all values in a single column MySQL Query?
- How to get a value more than a particular value from varchar column in MySQL?
- How to update a field with a particular value if it is null in MySQL?
- How to update MySQL column with random value?

Advertisements