
- 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
Set conditions while adding column values in MySQL?
To set conditions while adding column values, use MySQL IF(). Let us first create a table −
mysql> create table DemoTable -> ( -> Value1 int, -> Value2 int, -> Value3 int -> ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10,20,-30); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(50,60,90); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(100,200,400); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(30,40,-50); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(70,80,-1); Query OK, 1 row affected (0.24 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output −
+--------+--------+--------+ | Value1 | Value2 | Value3 | +--------+--------+--------+ | 10 | 20 | -30 | | 50 | 60 | 90 | | 100 | 200 | 400 | | 30 | 40 | -50 | | 70 | 80 | -1 | +--------+--------+--------+ 5 rows in set (0.00 sec)
Here is the query to set conditions while adding column values −
mysql> select Value1+Value2+if(Value3 > 0,Value3,0) from DemoTable;
This will produce the following output −
+---------------------------------------+ | Value1+Value2+if(Value3 > 0,Value3,0) | +---------------------------------------+ | 30 | | 200 | | 700 | | 70 | | 150 | +---------------------------------------+ 5 rows in set (0.00 sec)
- Related Articles
- Add a new column and set values in it on the basis of conditions in MySQL?
- Set particular value of column without using update and while inserting values in MySQL
- Set ENUM in MySQL for column values
- Adding characters in values for an existing int column in MySQL?
- Set conditions for columns with values 0 or 1 in MySQL
- Concatenate two values from the same column with different conditions in MySQL
- Can we skip a column name while inserting values in MySQL?
- Set DEFAULT values for columns while creating a table in MySQL
- Set conditions in a MySQL stored procedure
- Get sum of column with conditions in MySQL
- How to set all values in a single column MySQL Query?
- SET only two values for all the rows in a MySQL table based on conditions?
- Set a specific value for the first three column values in MySQL?
- MySQL UPDATE column names and set None values with N/A?
- Set column charset in MySQL?

Advertisements