MySQL 在插入触发器之前修改行值

MySQL modify row value on before insert trigger

我正在处理类似 Netflix 的数据库。我想创建一个触发器,只要将新内容添加到数据库中就会调用它。它应该做的是,如果内容的名称类似于“The Return of the King”,则将其替换为“Return of the King, The”。

目前我有以下触发器:

DROP TRIGGER IF EXISTS `UpdateContentName`;
       CREATE DEFINER=`root`@`localhost` TRIGGER `UpdateContentName` 
       BEFORE INSERT ON `content` FOR EACH ROW  
       BEGIN
            SET NEW.`name_content` = 'teeeeeest' 
            WHERE content.name_content LIKE = "The %";
        END

但我收到以下错误消息:

MySQL said: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE content.name_content LIKE = "The %"; END' at line 5

看起来问题出在 WHERE content.name_content LIKE = "The %"; 行,因为它在没有它的情况下也能正常工作,但我不明白我应该做什么...我花了几个小时试图解决这个问题但没有设法找到解决方案...

总而言之,我想实现如下目标:

If(name_content.contains("The %")) 
   //name_content in the form "name of the content, The" instead of "The name of the content"

感谢您的帮助! :)

您需要在设置之前检查条件

DELIMITER $$
DROP TRIGGER IF EXISTS `UpdateContentName`;
CREATE DEFINER=`root`@`localhost` TRIGGER `UpdateContentName` 
BEFORE INSERT ON `content` FOR EACH ROW  
BEGIN
    IF  NEW.name_content LIKE "The %" THEN
        SET NEW.`name_content` = 'teeeeeest';
    END IF;     
END$$
DELIMITER ;
CREATE tABLE content( name_content varchar(100))
CREATE  TRIGGER `UpdateContentName` 
BEFORE INSERT ON `content` FOR EACH ROW  
BEGIN
    IF  NEW.name_content LIKE "The %" THEN
        SET NEW.`name_content` = 'teeeeeest';
    END IF;     
END
INSERT INTO content VALUES('The AbC')
SELECT * FROM content
| name_content |
| :----------- |
| teeeeeest    |

db<>fiddle here