我正在尝试在触发器上使用算术,但我的触发器不起作用

I'm trying to use arithmatic on trigger, but my Trigger doesn't work

DELIMITER //
CREATE TRIGGER afterPembelian
AFTER INSERT ON pembelian

FOR EACH ROW BEGIN
DECLARE harga INT(20);
DECLARE total INT(20);

SELECT harga_obat INTO harga FROM daftar_obat WHERE kode_obat=NEW.kodeobat;

IF(DAYNAME(NEW.tanggal_pem)='Monday') THEN
    total = (harga-harga/10)*NEW.jml_beli;
ELSE 
    total = harga * NEW.jml_beli;
END IF;

SELECT * FROM daftar_obat;
SELECT * FROM pembelian;
SELECT total;

END//
DELIMITER ;

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= (harga-harga/10)*NEW.jml_beli; ELSE total = harga * NEW.jml_beli; END IF;' at line 11

我怀疑您需要一个 before 插入触发器,以便您可以更改行中的列值。像这样:

CREATE TRIGGER afterPembelian BEFORE INSERT ON pembelian
FOR EACH ROW
BEGIN
    DECLARE harga INT(20);
    DECLARE total INT(20);

    SELECT harga_obat INTO harga
    FROM daftar_obat o
    WHERE o.kode_obat = NEW.kodeobat;

    IF (DAYNAME(NEW.tanggal_pem) = 'Monday') THEN
        SET NEW.total = (harga  -harga / 10) * NEW.jml_beli;
    ELSE 
        SET NEW.total = harga * NEW.jml_beli;
    END IF;
END//
DELIMITER ;