我想将函数的返回值添加到表的列中 它应该都在mysql没有PHP 我有一个价格,税和价格与税的表我实现了计算含税价格的功能,但我不知道如何将这些值添加到该表
CREATE TABLE Faturat
(Fatura_ID varchar(10),
price real
,tax real,
CmimiMeTvsh real as (total(price,tax)),
Data varchar(20),
PRIMARY KEY (Fatura_ID));
drop function if exists total;
delimiter //
create function total( x real, y real)
returns real
begin
return X+x*Y/100;
select fatura_id,total(price,tax)
from FATURAT;
CREATE TRIGGER trgAuditOnInsertPunetoret AFTER INSERT ON faturat
FOR EACH ROW
BEGIN
INSERT INTO faturat
VALUES (fatura_id,price,tax,total(price,Tax),data);
end ;
delimiter //
答案 0 :(得分:0)
此处可能只需要生成的列
drop table if exists t;
create table t(id int auto_increment primary key, price int, tax int, pricetax int as (price + price *tax/100));
insert into t (price,tax) values (100,6);
select * from t;
+----+-------+------+----------+
| id | price | tax | pricetax |
+----+-------+------+----------+
| 1 | 100 | 6 | 106 |
+----+-------+------+----------+
1 row in set (0.00 sec)
BTW从内存中我不认为生成的列中允许用户定义的函数,并且你的触发器不起作用 - 你不能操作触发触发器的表。