user3894971
user3894971

Reputation: 1

MySql Trigger on table

UPDATE zapisy SET grupa =(
SELECT 
CASE
    WHEN ( (YEAR(CURDATE())- YEAR(data_urodzenia)) >= 18 AND (YEAR(CURDATE())- YEAR(data_urodzenia)) <=29) THEN 1
    WHEN ( (YEAR(CURDATE())- YEAR(data_urodzenia)) >= 29 AND (YEAR(CURDATE())- YEAR(data_urodzenia)) <=39) THEN 2
    WHEN ( (YEAR(CURDATE())- YEAR(data_urodzenia)) >= 39 AND (YEAR(CURDATE())- YEAR(data_urodzenia)) <=49) THEN 3
    WHEN ( (YEAR(CURDATE())- YEAR(data_urodzenia)) >= 49 AND (YEAR(CURDATE())- YEAR(data_urodzenia)) <=59) THEN 4
    ELSE 5
 END as row
 FROM `zapisy` 
 )

How to do trigger with this?

Upvotes: 0

Views: 60

Answers (1)

StackCoder
StackCoder

Reputation: 87

It seems that you can't do all this in a trigger. According to the documentation:

    Within a stored function or trigger, it is not permitted to modify a table 
    that is already being used (for reading or writing) by the statement that 
    invoked the function or trigger.

According to this answer, it seems that you should:

    create a stored procedure, that inserts into/Updates the target table, 
    then updates the other row(s), all in a transaction.

Upvotes: 1

Related Questions