将一列与不同表中的另一列进行比较并将值输入到另一列触发器 MySQL
Comparing one column to another column in different tables and inputting the value into another column trigger MySQL
我有一个 table 会员。一列称为 amount_paid,另一列称为 valid_membership。我有另一个名为 m_type 的 table,其中有一列名为价格。链接子句是 WHERE membership.type_id = m_type.type_id
我想要在加入会员资格之前触发 table 以检查支付的金额是否 >= 价格的 1/2。如果它更大,我希望将 1 放置在 valid_membership 中,否则如果它不正确,则将 0 放置在 valid_membership.
中
只是在正确语法方面遇到了一些麻烦,这是我已经尝试过的--
DELIMITER
//
CREATE TRIGGER valid_membership
BEFORE INSERT ON membership
FOR EACH ROW
BEGIN
IF (NEW.amount_paid >= 1/2 price) THEN
SET valid_membership = '1' ELSE '0'
WHERE membership.type_id = m_type.type_id
END IF;
END
//
DELIMITER ;
DELIMITER //
CREATE TRIGGER valid_membership
BEFORE INSERT ON membership
FOR EACH ROW
BEGIN
SELECT
price INTO @price
FROM
m_type
WHERE
type_id = NEW.type_id
AND ... -- something missing here as linking by the type_id only is too wide, imho
;
SET NEW.valid_membership = IF(NEW.amount_paid >= @price / 2, 1, 0);
END
//
DELIMITER ;
我有一个 table 会员。一列称为 amount_paid,另一列称为 valid_membership。我有另一个名为 m_type 的 table,其中有一列名为价格。链接子句是 WHERE membership.type_id = m_type.type_id
我想要在加入会员资格之前触发 table 以检查支付的金额是否 >= 价格的 1/2。如果它更大,我希望将 1 放置在 valid_membership 中,否则如果它不正确,则将 0 放置在 valid_membership.
中只是在正确语法方面遇到了一些麻烦,这是我已经尝试过的--
DELIMITER
//
CREATE TRIGGER valid_membership
BEFORE INSERT ON membership
FOR EACH ROW
BEGIN
IF (NEW.amount_paid >= 1/2 price) THEN
SET valid_membership = '1' ELSE '0'
WHERE membership.type_id = m_type.type_id
END IF;
END
//
DELIMITER ;
DELIMITER //
CREATE TRIGGER valid_membership
BEFORE INSERT ON membership
FOR EACH ROW
BEGIN
SELECT
price INTO @price
FROM
m_type
WHERE
type_id = NEW.type_id
AND ... -- something missing here as linking by the type_id only is too wide, imho
;
SET NEW.valid_membership = IF(NEW.amount_paid >= @price / 2, 1, 0);
END
//
DELIMITER ;