

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Change value from 1 to Y in MySQL Select Statement using CASE?
You can use CASE from MySQL to change value from 1 to Y. Let us first create a table. The query to create a table is as follows:
mysql> create table changeValuefrom1toY -> ( -> Id int NOT NULL AUTO_INCREMENT, -> isValidAddress tinyint(1), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.76 sec)
Now you can insert some records in the table using insert command. The query is as follows:
mysql> insert into changeValuefrom1toY(isValidAddress) values(1); Query OK, 1 row affected (0.22 sec) mysql> insert into changeValuefrom1toY(isValidAddress) values(0); Query OK, 1 row affected (0.16 sec) mysql> insert into changeValuefrom1toY(isValidAddress) values(1); Query OK, 1 row affected (0.19 sec) mysql> insert into changeValuefrom1toY(isValidAddress) values(1); Query OK, 1 row affected (0.15 sec) mysql> insert into changeValuefrom1toY(isValidAddress) values(1); Query OK, 1 row affected (0.16 sec) mysql> insert into changeValuefrom1toY(isValidAddress) values(0); Query OK, 1 row affected (0.12 sec) mysql> insert into changeValuefrom1toY(isValidAddress) values(1); Query OK, 1 row affected (0.49 sec)
Display all records from the table using select statement. The query is as follows:
mysql> select *from changeValuefrom1toY;
The following is the output:
+----+----------------+ | Id | isValidAddress | +----+----------------+ | 1 | 1 | | 2 | 0 | | 3 | 1 | | 4 | 1 | | 5 | 1 | | 6 | 0 | | 7 | 1 | +----+----------------+ 7 rows in set (0.00 sec)
The following is the query to work with CASE:
mysql> select *,case -> when isValidAddress=1 THEN 'Y' -> ELSE isValidAddress -> END AS Answer -> from changeValuefrom1toY;
The following is the output:
+----+----------------+--------+ | Id | isValidAddress | Answer | +----+----------------+--------+ | 1 | 1 | Y | | 2 | 0 | 0 | | 3 | 1 | Y | | 4 | 1 | Y | | 5 | 1 | Y | | 6 | 0 | 0 | | 7 | 1 | Y | +----+----------------+--------+ 7 rows in set (0.00 sec)
- Related Questions & Answers
- Change value from 1 to Y in MySQL Select Statement?
- MySQL case statement inside a select statement?
- How to select return value from MySQL prepared statement?
- Storing value from a MySQL SELECT statement to a variable?
- Using CASE statement in MySQL to display custom name for empty value
- Change tinyint default value to 1 in MySQL?
- MySQL order by field using CASE Statement
- Perform case insensitive SELECT using MySQL IN()?
- MySQL Query to change lower case to upper case?
- Select records from MySQL NOW() -1 Day?
- How to use MySQL CASE statement while using UPDATE Query?
- Using the value of an alias inside the same MySQL SELECT statement
- Get table names using SELECT statement in MySQL?
- Using Prepared statement correctly with WHERE condition in case of any value in MySQL Java
- How to check if value exists with MySQL SELECT 1?
Advertisements