
- 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
Select from another column if selected value is '0' in MySQL?
For this, use IF() in MySQL. The syntax is as follows −
select IF(yourColumnName1=0,yourColumnName2,yourColumnName1) as anyAliasName from yourTableName;
Let us create a table −
mysql> create table demo30 −> ( −> id int not null auto_increment primary key, −> value int, −> original_value int −> ) −> ; Query OK, 0 rows affected (1.87 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo30(value,original_value) values(50,10); Query OK, 1 row affected (0.10 sec) mysql> insert into demo30(value,original_value) values(1000,0); Query OK, 1 row affected (0.13 sec) mysql> insert into demo30(value,original_value) values(0,40); Query OK, 1 row affected (0.15 sec) mysql> insert into demo30(value,original_value) values(30,0); Query OK, 1 row affected (0.09 sec)
Display records from the table using select statement −
mysql> select *from demo30;
This will produce the following output −
+----+-------+----------------+ | id | value | original_value | +----+-------+----------------+ | 1 | 50 | 10 | | 2 | 1000 | 0 | | 3 | 0 | 40 | | 4 | 30 | 0 | +----+-------+----------------+ 4 rows in set (0.00 sec)
Following is the query to select from another column if selected value is '0' in MySQL −
mysql> select id, if(original_value= 0,value,original_value) as Result −> from demo30;
This will produce the following output −
+----+--------+ | id | Result | +----+--------+ | 1 | 10 | | 2 | 1000 | | 3 | 40 | | 4 | 30 | +----+--------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL query to select rows where column value is only 0, group by another column?
- How to select distinct value from one MySQL column only?
- Show column value twice in MySQL Select?
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Return value from a row if it is NOT NULL, else return the other row value in another column with MySQL
- MySQL select to count values equal to 0 and greater than 0 from a column?
- Exclude rows based on column value when another duplicate column value is found in MySQL?
- MySQL SELECT to sum a column value with previous value
- Get first date from timestamp in MySQL group by another column with duplicate value
- Perform multiplication in SELECT depending on column value in MySQL?
- Update one column data to another column in MySQL if the second column is NOT NULL?
- Select a specific value between two column values in MySQL?
- Select a value from MySQL database only if it exists only once from a column with duplicate and non-duplicate values
- SELECT not null column from two columns in MySQL?
- Insert values in a table by MySQL SELECT from another table in MySQL?

Advertisements