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)