
- 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
Change value from 1 to Y in MySQL Select Statement?
You can use IF() from MySQL to change value from 1 to Y. The syntax is as follows:
SELECT IF(yourColumnName,’Y’,yourColumnName) as anyVariableName FROM yourTableName;
To understand the above syntax, let us 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)
Here is the query to change the value from 1 to Y. The query is as follows:
mysql> select if(isValidAddress,'Y',isValidAddress) as Answer from changeValuefrom1toY;
The following is the output:
+--------+ | Answer | +--------+ | Y | | 0 | | Y | | Y | | Y | | 0 | | Y | +--------+ 7 rows in set (0.00 sec)
- Related Articles
- Change value from 1 to Y in MySQL Select Statement using CASE?
- How to select return value from MySQL prepared statement?
- Storing value from a MySQL SELECT statement to a variable?
- Change tinyint default value to 1 in MySQL?
- MySQL case statement inside a select statement?
- How to use NULL in MySQL SELECT statement?
- How to change MySQL ending statement?
- How to check if value exists with MySQL SELECT 1?
- Select records from MySQL NOW() -1 Day?
- How to create Tab Delimited Select statement in MySQL?
- IF() function in a MySQL Select statement?
- Using the value of an alias inside the same MySQL SELECT statement
- How to select distinct value from one MySQL column only?
- MySQL select statement to fetch 5 characters from the left of the string
- MySQL procedure to display a “select” statement twice

Advertisements