
- 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 replace values in a table?
To replace values in a table, use the CASE statement. To understand the concept, let us create a table. The query to create a table is as follows
mysql> create table replaceValueDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(10), -> isGreaterThan18 varchar(10) -> ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into replaceValueDemo(Name,isGreaterThan18) values('John','YES'); Query OK, 1 row affected (0.24 sec) mysql> insert into replaceValueDemo(Name,isGreaterThan18) values('Carol','NO'); Query OK, 1 row affected (0.16 sec) mysql> insert into replaceValueDemo(Name,isGreaterThan18) values('Mike','YES'); Query OK, 1 row affected (0.20 sec) mysql> insert into replaceValueDemo(Name,isGreaterThan18) values('Bob','YES'); Query OK, 1 row affected (0.17 sec) mysql> insert into replaceValueDemo(Name,isGreaterThan18) values('Larry','NO'); Query OK, 1 row affected (0.09 sec) mysql> insert into replaceValueDemo(Name,isGreaterThan18) values('David','NO'); Query OK, 1 row affected (0.11 sec) mysql> insert into replaceValueDemo(Name,isGreaterThan18) values('James','DONOTKNOW'); Query OK, 1 row affected (0.14 sec) mysql> insert into replaceValueDemo(Name,isGreaterThan18) values('Robert','DONOTKNOW'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from replaceValueDemo;
The following is the output
+----+--------+-----------------+ | Id | Name | isGreaterThan18 | +----+--------+-----------------+ | 1 | John | YES | | 2 | Carol | NO | | 3 | Mike | YES | | 4 | Bob | YES | | 5 | Larry | NO | | 6 | David | NO | | 7 | James | DONOTKNOW | | 8 | Robert | DONOTKNOW | +----+--------+-----------------+ 8 rows in set (0.00 sec)
Here is the query to replace values in a MySQL table
mysql> update replaceValueDemo set isGreaterThan18=case when isGreaterThan18='YES' THEN 'NO' ELSE 'YES' END -> where isGreaterThan18 IN('YES','NO'); Query OK, 6 rows affected (0.19 sec) Rows matched: 6 Changed: 6 Warnings: 0
Let us check the table records once again.
The query is as follows
mysql> select *from replaceValueDemo;
The following is the output with the replaced values
+----+--------+-----------------+ | Id | Name | isGreaterThan18 | +----+--------+-----------------+ | 1 | John | NO | | 2 | Carol | YES | | 3 | Mike | NO | | 4 | Bob | NO | | 5 | Larry | YES | | 6 | David | YES | | 7 | James | DONOTKNOW | | 8 | Robert | DONOTKNOW | +----+--------+-----------------+ 8 rows in set (0.00 sec)
- Related Articles
- Replace the empty values from a MySQL table with a specific value
- MySQL query to replace only the NULL values from the table?
- In MySQL how to replace all NULL values in a particular field of a particular table?
- How to replace a character in a MySQL table?
- How to replace rows in a MySQL table with conditions?
- Replace part of string in MySQL table column?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- Find and Replace text in the entire table using a MySQL?
- Updating a MySQL table with values from another table?
- Reshuffle the values in a table with MySQL
- How to replace values of select return in MySQL?
- How can we update values in a MySQL table?
- Auto insert values into a MySQL table in a range?
- How can we update the values in one MySQL table by using the values of another MySQL table?
- Populate null columns in a MySQL table and set values

Advertisements