如何在php myadmin中创建触发器

时间:2015-09-15 07:50:02

标签: mysql triggers

如何在phpmyadim中使用join和if if条件.. MySQL Trigger对我来说是全新的...... 这是我的触发器

DROP TRIGGER IF EXISTS `visit`;
CREATE DEFINER=`root`@`localhost` TRIGGER `visit` 
AFTER INSERT ON `daily_call_reports` 
FOR EACH ROW
    Update dealers d 
    JOIN daily_call_reports dcr
              ON d.dealer_id = dcr.client_id

         IF d.visit  IS NULL THEN 
                SET d.visit  = 1

          else  
                SET d.visit  = visit+1

          WHERE  dcr.client_id = NEW.client_id

但是当我点击go按钮时会显示如下错误

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 'IF d.visit IS NULL THEN SET d.visit = 1 
els' at line 5

感谢先进..

3 个答案:

答案 0 :(得分:0)

试试这个(语法)

DELIMITER $$

CREATE TRIGGER trigger_name after insert ON `table1` FOR EACH ROW BEGIN  INSERT  INTO `table2` (column1,column2) VALUES (NEW.column1,NEW.cllumn2);

END $$

 DELIMITER ;

答案 1 :(得分:0)

你需要在触发器中使用Delimiter,因为默认分隔符即分号(;)不起作用。

请参阅此链接以了解分隔符:

why to use Delimiter?

答案 2 :(得分:0)

更新语句不正确,因为您不能按照尝试的方式在更新命令中使用if-else。您可以将更新部分更改为

update dealers d 
join daily_call_reports dcr on d.dealer_id = dcr.client_id
set d.visit  = 
case when d.visit  IS NULL then 1 else visit+1 end
where dcr.client_id = NEW.client_id