MYSQL 在使用双引号和单引号时截断了不正确的 DOUBLE

问题描述

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

这是我的触发器

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"

但这并不好。

我做错了什么?

提前致谢。

解决方法

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

改用CONCAT()

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

并且,使用 ' 对您想要的字符串中的 \ 字符进行转义。

'Mrs. O\'Leary\'s cow.'