Gossamer Forum
Home : General : Perl Programming :

Calculated Field in MySQL

Quote Reply
Calculated Field in MySQL
Hi-

Does anyone know how to create a field in MySQL that is a calculated value (and that is stored in the table)?

For example if I have two fields for a record in a table:

number (value = 2)
rate (value = 5)

and I want to create a calculated field in the same MySQL table called: total

and this calculated field 'total' would have the value of 'number' multiplied by 'rate'.

total (value = 10)

Is it possible to do this, such that the value in the 'total' field would change according to what is entered or changed in either the 'number' or 'rate' field?

Any help or comments would be much appreciated!

--Rover


Quote Reply
Re: Calculated Field in MySQL In reply to
You would use the following SQL statement in your script:

Code:

UPDATE tbl_Name
SET Total = '$total'


You would define the following variables above the SQL statement:

Code:

my $rate = $in->param('RateFieldName');
my $number = $in->param('NumberFieldName');
my $total = $rate X $number;


Regards,

Eliot Lee Wink
http://anthrotech.com/
Quote Reply
Re: Calculated Field in MySQL In reply to
Thanks very much!

Quote Reply
Re: Calculated Field in MySQL In reply to
there is no X operator in perl that i'm aware of.

to multiply you use *
the lowercase x is for string concats.
i.e.
my $char = 'A';
my $length = 5;
my $string = $char x $length;

will give string the value:
AAAAA

-g



s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';