![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
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)
Advertisements