- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL select any one field out of two with respect to the value of a third field?
For this, use IF(). Let us first create a table −
mysql> create table DemoTable -> ( -> PlayerName varchar(100), -> PlayerScore int, -> PlayerStatus varchar(100) -> ); Query OK, 0 rows affected (0.58 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John',88,'BAD'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris',78,'BAD'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Robert',90,'BAD'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David',80,'BAD'); Query OK, 1 row affected (0.23 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+------------+-------------+--------------+ | PlayerName | PlayerScore | PlayerStatus | +------------+-------------+--------------+ | John | 88 | BAD | | Chris | 78 | BAD | | Robert | 90 | BAD | | David | 80 | BAD | +------------+-------------+--------------+ 4 rows in set (0.00 sec)
Following is the query to select any one field out of two with respect to the value of a third field −
mysql> select PlayerScore,if(PlayerScore > 80 , PlayerName ,PlayerStatus) AS Result from DemoTable;
Output
+-------------+--------+ | PlayerScore | Result | +-------------+--------+ | 88 | John | | 78 | BAD | | 90 | Robert | | 80 | BAD | +-------------+--------+ 4 rows in set (0.00 sec)
- Related Articles
- How to derive value of a field from another field in MySQL?
- Concat a field in MySQL SELECT?
- How to select data in MySQL where a field has a minimum value?
- State any two properties of magnetic field lines.
- How to generate field in MySQL SELECT?
- Select a field and if it's null, select another with MySQL?
- MySQL query to fetch only a single field on the basis of boolean value in another field
- MySQL ORDER BY with custom field value
- Return records that do not have a value in a certain field with two SELECT statement in a single MySQL query
- Limit length of longtext field in MySQL SELECT results?
- Reset MySQL field to default value?
- How to select a field corresponding to the field in which MAX() exists?
- Update MongoDB field using value of another field?
- MongoDB query to select one field if the other is null?
- Concatenate strings from two fields into a third field in MongoDB?

Advertisements