0

我创建了一个触发器,该触发器在另一个表中删除一行后在表中插入一行。

这是我的触发器

CREATE TRIGGER trigger_delete_log_animal AFTER delete ON animal
FOR EACH ROW
    INSERT INTO log_animais (momento, ocorrencia)
    VALUES (now(), "The register '" + old.nome_animal + "' was deleted from the animal table");

我希望 nome_animal 在单引号之间。

但是当我从动物表中删除一行时出现以下错误:

Error Code: 1292. Truncated incorrect DOUBLE value: 'The register ''

我试过把它改成

'The register "' + old.nome_animal + '" was deleted from the animal table'

而且还要

"The register \'" + old.nome_animal + "\' was deleted from the animal table"

但这并不好。

我究竟做错了什么?

4

1 回答 1

1

不要尝试在 SQL 代码中使用 + 构建字符串。

改用CONCAT()

 VALUES (now(), CONCAT(
           'The register \'', 
            old.nome_animal, 
            '\' was deleted from the animal table'));

并且,使用 . 转义'字符串中所需的字符\

'Mrs. O\'Leary\'s cow.'
于 2021-06-11T12:54:39.240 回答