mysql 在插入数据查询中引用相关性数据列表

xzv2uavs  于 2022-12-03  发布在  Mysql
关注(0)|答案(1)|浏览(143)

我正在学习MySql数据库。我有两个表,如ServiceTable(服务ID(PK),...,费用)TransactionTable(事务ID(PK),服务ID(FK),...,费用)。我写一个produce将数据插入到TransactionTable中:

CREATE DEFINER = 'root'@'localhost'
PROCEDURE bbet.Proc_transactiontable_CreateTransaction(IN userID varchar(36),
IN serviceID varchar(36),
...,
IN amountMoney float
)
BEGIN
SELECT @fee = s.Fee FROM servicetypetable s WHERE s.ServiceID = serviceID;
INSERT INTO transactiontable (TransactionID, ServiceID,..., Fee, TotalMoney,  ModifiedDate)
VALUES (UUID(), serviceID, amountMoney,..., @fee, amountMoney * (1 - @fee), NOW());
END

我想从ServiceTable的Fee列引用produce中的数据,但是在这种情况下@fee为空。如何从ServiceTable的Fee列引用produce中的数据?

2ul0zpep

2ul0zpep1#

CREATE DEFINER = 'root'@'localhost'
PROCEDURE bbet.Proc_transactiontable_CreateTransaction(
    IN userID varchar(36),
    IN serviceID varchar(36),
    ...,
    IN amountMoney float
    )
INSERT INTO transactiontable (TransactionID, ServiceID,..., Fee, TotalMoney,  ModifiedDate)
SELECT UUID(), serviceID, amountMoney,..., s.Fee, amountMoney * (1 - s.Fee), NOW()
FROM servicetypetable s 
WHERE s.ServiceID = serviceID;

附言:我不建议你干预局部变量名和列名。例如,使用IN in_serviceID varchar(36)

相关问题