如何从mysql函数向列表添加数据

kiz8lqtg  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(290)

我想把一个函数的返回值加到一个表的列中它应该都在mysql中没有php我有一个带有price,tax和price with tax的表我实现了这个函数来计算带有tax的price,但是我不知道如何把这些值加到那个表中

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 //
kkbh8khc

kkbh8khc1#

这里可能只需要一个生成的列

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)

顺便说一句,我不认为在生成的列中允许使用用户定义的函数,您的触发器也不会工作-您不允许对触发触发器的表进行操作。

相关问题