Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column calculated from another column?

Tags:

sql

mysql

Given the following table:

id | value
--------------
1     6
2     70

Is there a way to add a column that is automatically calculated based on another column in the same table? Like a VIEW, but part of the same table. As an example, calculated would be half of value. Calculated should be automatically updated when value changes, just like a VIEW would be.

The result would be:

id | value | calculated
-----------------------
1     6       3
2     70      35
like image 820
Matthew Avatar asked Mar 07 '11 16:03

Matthew


People also ask

Can you use a calculated field in another calculated field?

About Calculated Fields A calculated field becomes a new field in the pivot table, and its calculation can use the sum of other fields. Calculated fields appear with the other value fields in the pivot table.

How do I make a column from another column in SQL?

In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table.

Can a calculated column reference another table?

A computed column may only reference other columns in the same table.


3 Answers

Generated Column is one of the good approach for MySql version which is 5.7.6 and above.

There are two kinds of Generated Columns:

  • Virtual (default) - column will be calculated on the fly when a record is read from a table
  • Stored - column will be calculated when a new record is written/updated in the table

Both types can have NOT NULL restrictions, but only a stored Generated Column can be a part of an index.

For current case, we are going to use stored generated column. To implement I have considered that both of the values required for calculation are present in table

CREATE TABLE order_details (price DOUBLE, quantity INT, amount DOUBLE AS (price * quantity));

INSERT INTO order_details (price, quantity) VALUES(100,1),(300,4),(60,8);

amount will automatically pop up in table and you can access it directly, also please note that whenever you will update any of the columns, amount will also get updated.

like image 183
Abhishek Gupta Avatar answered Oct 21 '22 16:10

Abhishek Gupta


If it is a selection, you can do it as:

SELECT id, value, (value/2) AS calculated FROM mytable

Else, you can also first alter the table to add the missing column and then do an UPDATE query to compute the values for the new column as:

UPDATE mytable SET calculated = value/2;

If it must be automatic, and your MySQL version allows it, you can try with triggers

like image 49
Vincent Mimoun-Prat Avatar answered Oct 21 '22 14:10

Vincent Mimoun-Prat


MySQL 5.7 supports computed columns. They call it "Generated Columns" and the syntax is a little weird, but it supports the same options I see in other databases.

https://dev.mysql.com/doc/refman/5.7/en/create-table.html#create-table-generated-columns

like image 24
Jonathan Allen Avatar answered Oct 21 '22 16:10

Jonathan Allen