
- 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
Add a new column and set values in it on the basis of conditions in MySQL?
To set values on the basis of conditions, use IF() method. Let us first create a table −
mysql> create table DemoTable -> ( -> Age int -> ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(19); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(16); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(17); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(22); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Ouptut
This will produce the following output −
+------+ | Age | +------+ | 19 | | 16 | | 17 | | 22 | +------+ 4 rows in set (0.00 sec)
Following is the query to set values on the basis of condition −
mysql> select Age,if(Age > 18,'You are qualified!','Sorry, you are rejected!') AS Result from DemoTable;
Output
This will produce the following output −
+------+-------------------------+ | Age | Result | +------+-------------------------+ | 19 | You are qualified! | | 16 | Sorry, you are rejected!| | 17 | Sorry, you are rejected!| | 22 | You are qualified! | +------+-------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Display custom text in a new column on the basis of null values in MySQL?
- Set conditions while adding column values in MySQL?
- Match column values on the basis of the other two column values in MySQL
- Fetching records from a table with NULL and other values on the basis of conditions in MySQL
- Multiply values of two columns and display it a new column in MySQL?
- Enter values with prompt and evaluate on the basis of conditions in JavaScript?
- Add a column count in a MySQL query on the basis of last name records?
- Set custom messages on the basis of a column with student marks in MySQL
- Concatenate rows on the basis of boolean values in another column with MySQL
- Set the NULL values to 0 and display the entire column in a new column with MySQL SELECT
- Add a temporary column in MySQL where the values depend on another column?
- SET only two values for all the rows in a MySQL table based on conditions?
- Set new delay time in a MySQL column
- MySQL SELECT to add a new column to a query and give it a value?
- How can I create a stored procedure to select values on the basis of some conditions from a MySQL table?

Advertisements