MYSql触发器语句

时间:2015-12-16 18:36:10

标签: mysql triggers

我似乎无法得到其中一个触发器,我试图在它看起来像这样的时刻工作(已经被搞砸了很多,试图让它工作,所以可能没有任何意义现在)

CREATE TRIGGER `insertproductebay` AFTER INSERT ON `product`
FOR EACH ROW INSERT INTO eBayLinked
(product_id,company_id,eBay_ID,ebay_token_id,ebay_username) 
SELECT ebay_token_id,ebay_username
FROM
'company'
WHERE
'company_id' = 'company_id'
  VALUES
      (NEW.product_id,NEW.company_id,NEW.eBay_ID,ebay_token_id,ebay_username)

2 个答案:

答案 0 :(得分:1)

以下内容超过了1065错误

必须注意在表格和列名称周围使用反向标记,而不是在那里使用单引号。

drop trigger if exists `insertproductebay`;

DELIMITER $$
CREATE TRIGGER `insertproductebay` AFTER INSERT ON `product`
FOR EACH ROW 
INSERT INTO eBayLinked
(product_id,company_id,eBay_ID,ebay_token_id,ebay_username) 
SELECT NEW.product_id,NEW.company_id,NEW.eBay_ID,ebay_token_id,ebay_username
FROM `company`
WHERE `company_id` = 'company_id'
$$
DELIMITER ;

我认为剩下的问题可能是你的意思

WHERE `company_id` = 'company_id'
触发器中的

。因为触发器是一个在后台运行的无代码代码,无声地成功或失败。换句话说,没有与之关联的用户界面。

答案 1 :(得分:0)

您不能以这种方式混合INSERT ... SELECTINSERT ... VALUES语法。但是,您可以在选择列表中提供文字值,因此:

CREATE TRIGGER insertproductebay AFTER INSERT ON product FOR EACH ROW
  INSERT INTO eBayLinked
    (product_id, company_id, eBay_ID, ebay_token_id, ebay_username) 
  SELECT NEW.product_id, NEW.company_id, NEW.eBay_ID, ebay_token_id, ebay_username
  FROM   company
  WHERE  company_id = NEW.company_id