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`
 )


如何触发呢?

最佳答案

看来您无法在触发器中完成所有这些操作。根据文档:

    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.


根据此答案,您似乎应该:

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

关于mysql - 表格上的MySQL触发器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25055734/

10-15 18:30