mysql正在尝试从max value获取其他属性

kninwzqo  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(296)

我有这个扳机

DELIMITER $$
CREATE TRIGGER before_bid_insert 
BEFORE INSERT ON Bids
FOR EACH ROW 
BEGIN

IF NEW.amount > (SELECT B.amount, B.vin FROM Bids B WHERE B.vin = NEW.vin AND B.amount = (SELECT MAX(amount) from Bids))  THEN
INSERT INTO Notifications (buyer, outbidded, vin, amount) values (NEW.buyer, B.buyer, NEW.vin, NEW.amount);

END IF ;

END$$
DELIMITER ;

有table的

CREATE TABLE Auctions
(seller VARCHAR(100),
vin VARCHAR(20),
brand VARCHAR(20),
cartype VARCHAR(20),
model VARCHAR(20),
color VARCHAR(20),
minprice int,
dt DATETIME,
PRIMARY KEY (vin, seller)
);

CREATE TABLE Bids
(buyer VARCHAR(20),
vin VARCHAR(20),
amount int,
autobid int,
upperlimit int,
PRIMARY KEY(buyer, vin, amount),
FOREIGN KEY (vin) REFERENCES Auctions(vin)

);

CREATE TABLE Notifications
(buyer VARCHAR(20),
outbidded VARCHAR(20),
vin VARCHAR(20),
amount int,
PRIMARY KEY(vin, buyer),
FOREIGN KEY (vin) REFERENCES Bids(vin),
FOREIGN KEY (buyer) REFERENCES Bids(buyer)
);

不过,每当我运行它时,它都会说操作数应该包含1列。
我要做的是,如果要插入的行的amount值比bids表中与新行具有相同vin的任何行的amount值都高,那么将这些值插入到通知中。

uqxowvwt

uqxowvwt1#

您可以这样做来帮助解决这两个问题:

SELECT B.amount INTO @amount, B.buyer INTO @buyer 
    FROM Bids B WHERE B.vin = NEW.vin AND B.amount = (SELECT MAX(amount) from Bids)

IF NEW.amount > @amount  THEN
INSERT INTO Notifications (buyer, outbidded, vin, amount)
values (NEW.buyer, @buyer, NEW.vin, NEW.amount);

另外,不确定,但我觉得你应该再办理一次入住手续 SELECT MAX(amount) from Bids 查询以包含 WHERE 要检查的子句 vin .

相关问题