Question

Specifically, I'd like to insert a third column (c3) that equals c1 divided by c2 rounded to the nearest integer. I've looked at mySql cookbooks and skimmed through O' Reilly's introduction, but I've yet to see anything that would suggest this is possible.

Using JDBC, Apache's DBUtils, or a simple mysql script are all possibilities for me.

Was it helpful?

Solution

Create a view on the base table:

create view myview as
select c1, c2, round(c1 / c2) as c3
from mytable

You can treat myview like a regular table, except you won't be allowed to update or insert values in the calculated column.

OTHER TIPS

If you are looking for one-time exercise, you can just add new column through ALTER TABLE and then run UPDATE query with calculation formula it will work.

If you are looking for the solution that new column should have calculated value, you can create TRIGGER on Insert & Update operations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top