3

这是我试图用来创建触发器的代码:

-- Dumping structure for trigger kavanga_lead.click_links_insert
DROP TRIGGER IF EXISTS `click_links_insert`;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='';
DELIMITER //
CREATE TRIGGER `click_links_insert` AFTER INSERT ON `click_links` FOR EACH ROW BEGIN
    INSERT INTO actions_log 
        (
            `uid`,
            `table`,
            `action`,
            `new`
        )
        VALUES
        (  
            @user_id,
            'click_links',
            'insert',
            concat(NEW.id ,'|', NEW.`contents`, '|', NEW.channel_id, '|', NEW.name, '|', NEW.hidden, '|', if(NEW.prefix_id is null,'',NEW.prefix_id) '|', if(NEW.postfix_id is null,'',NEW.postfix_id))
        );
END//
DELIMITER ;
SET SQL_MODE=@OLD_SQL_MODE;

我不断收到错误 (1583) Incorrect parameters in the call to concat。如果我不使用if(NEW.prefix_id is null,'',NEW.prefix_id)但只使用NEW.prefix_id. 但是这个字段可以null,所以所有concat的结果都变成null了to。

所以,问题是我如何IFCONCAT调用中使用语句?

4

3 回答 3

5

那是因为您在 尝试comma后忘记了一个:if(NEW.prefix_id is null,'',NEW.prefix_id)

concat(NEW.id ,'|', NEW.`contents`, '|', NEW.channel_id, '|', NEW.name, '|', NEW.hidden, '|', if(NEW.prefix_id is null,'',NEW.prefix_id), '|', if(NEW.postfix_id is null,'',NEW.postfix_id))
于 2012-08-02T11:27:05.507 回答
2

你的 . 后面少了一个逗号IF。解决此问题后,最好改用COALESCE(如果您愿意,也可以使用 IFNULL)

COALESCE(NEW.prefix_id,  '')
于 2012-08-02T11:28:32.060 回答
0

您的查询无效,因为comma您的inline IF声明后缺少。

concat(NEW.id ,'|', NEW.`contents`, '|', 
       NEW.channel_id, '|', NEW.name, '|', NEW.hidden, '|', 
       if(NEW.prefix_id is null,'',NEW.prefix_id) '|', 
       --                                         ^ error here
       if(NEW.postfix_id is null,'',NEW.postfix_id))

肯定是

concat(NEW.id ,'|', NEW.`contents`, '|', 
       NEW.channel_id, '|', NEW.name, '|', NEW.hidden, '|', 
       if(NEW.prefix_id is null,'',NEW.prefix_id), '|', 
       if(NEW.postfix_id is null,'',NEW.postfix_id))
于 2012-08-02T11:32:48.040 回答