Ijimero
Ijimero

Reputation: 83

MySQL Trigger IF Statement OR not workign when two conditions match

I'm fairly new to MySQL Trigger sintax and I'm having a problem that I could not find a clear answer anywhere.

i have a quite simple IF statment, it enters on the IF, if one OR the other condition is met, but never if both are.

i want to have some action going if one field or other field of the updated table changes, but if i update both fields it does not work.

Keep in mind that this trigger happens AFTER UPDATE of the table here's my SAMPLE code for easier visualization:

BEGIN
    IF NEW.field1 <=> OLD.field1 OR NEW.field2 <=> OLD.field2 THEN
        #action happens here
    END IF;
END

The ACTUAL code:

BEGIN
    IF NEW.margem_consig <=> OLD.margem_consig OR NEW.margem_cartao <=> OLD.margem_cartao THEN
        INSERT INTO historico_margem (idmatricula, margem, margem_cartao, margem_confiavel, margem_anterior_con, margem_anterior_car)
        VALUES (NEW.idmatricula, NEW.margem_consig, NEW.margem_cartao, NEW.margem_confiavel, OLD.margem_consig, OLD.margem_cartao);
    END IF;
END

So, if on update BOTH fields 1 and 2 changes, the condition is never met, can anybody shed me some light?

Also, sorry for any misspelling

Upvotes: 2

Views: 873

Answers (2)

Ijimero
Ijimero

Reputation: 83

The problem was the <=> operator, replaced it with != and it worked as expected.

thanks for everyone's time!

CORRECTED SAMPLE CODE:

BEGIN
    IF NEW.field1 != OLD.field1 OR NEW.field2 != OLD.field2 THEN
        #action happens here
    END IF;
END

Upvotes: 1

Ananta
Ananta

Reputation: 700

To test difference please use the <> operator.

BEGIN
    IF NEW.field1 <> OLD.field1 OR NEW.field2 <> OLD.field2 THEN
        #action happens here
    END IF;
END

Upvotes: 0

Related Questions