
- 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
How to derive value of a field from another field in MySQL?
For this, you can use the concept of user defined variable. Let us first create a table −
mysql> create table DemoTable1868 ( Value int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1868 values(10); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1868 values(20); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1868 values(30); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1868 values(40); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1868;
This will produce the following output −
+-------+ | Value | +-------+ | 10 | | 20 | | 30 | | 40 | +-------+ 4 rows in set (0.00 sec)
Here is the query to derive value of a field from another field in MySQL −
mysql> select Value,(@iterator:=Value+@iterator) as SecondColumn from DemoTable1868,( select @iterator:=0) tbl;
This will produce the following output −
+-------+--------------+ | Value | SecondColumn | +-------+--------------+ | 10 | 10 | | 20 | 30 | | 30 | 60 | | 40 | 100 | +-------+--------------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL query to fetch only a single field on the basis of boolean value in another field
- Update MongoDB field using value of another field?
- How to swap a specific field value in MySQL?
- MySQL query to count comma’s from field value?
- How to remove leading and trailing whitespace from a MySQL field value?
- How to set default Field Value in MySQL?
- How to copy data from one field to another on every row in MySQL?
- Getting the maximum value from a varchar field in MySQL
- How to sum based on field value in MySQL?
- Reset MySQL field to default value?
- How to update field to add value to existing value in MySQL?
- How to convert char field to datetime field in MySQL?
- How to perform custom sort by field value in MySQL?
- Insert datetime into another datetime field in MySQL?
- How to get an age from a D.O.B field in MySQL?

Advertisements