Create Table Billing3
(
billingId int primary key,
FoodCharge float DEFAULT 0,
DoctorCharge float DEFAULT 0,
TestCharge float DEFAULT 0,
OperationCharge float DEFAULT 0,
RoomCharge float DEFAULT 0,
Total float DEFAULT (FoodCharge + DoctorCharge + TestCharge + OperationCharge + RoomCharge)
)
Alternatively, you can set up a MySQL insert trigger . But usually, you want to keep calculations in queries as you save on storage and avoid maintenance on a programming object. Plus, if one of the charges updates values, you would then need an update trigger.
USE `databasename`;
DELIMITER
$$
CREATE TRIGGER `TotalCalculation`
BEFORE INSERT
ON `Billing3` FOR EACH ROW
-- Edit trigger body code below this line. Do not edit lines above this one
BEGIN
SET NEW.Total = NEW.FoodCharge + NEW.DoctorCharge + NEW.TestCharge +
NEW.OperationCharge + NEW.RoomCharge;
END
$$
MySQL does not support calculated columns. You can use a view for this purpose:
create view v_billing3 as
select b.*,
(FoodCharge + DoctorCharge + TestCharge + OperationCharge + RoomCharge) as total
from billing3 b;
Also, don't store numbers as floating points. Instead, use the fixed point type, decimal
, for this purpose.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.