我正在尝试执行此操作。它给了我mysql语法的错误。无法弄清楚原因。
CREATE TRIGGER trig1
AFTER INSERT OR UPDATE ON table1
FOR EACH ROW
INSERT INTO table2 (id,entity_type,`data`,processed,created_at,last_updated_at )
VALUES ( DEFAULT , 'campaigns',
'{"campaign_id":"' CONCAT NEW.campaign_id CONCAT '","name":"' CONCAT NEW.name CONCAT '","advertiser_id":"' CONCAT NEW.advertiser_id CONCAT '","start_date":"' CONCAT NEW.start_date
CONCAT '","end_date":"' CONCAT NEW.end_date CONCAT '","status":"' CONCAT NEW.status CONCAT '","pacing":"' CONCAT NEW.pacing CONCAT '","budget_value":"' CONCAT NEW.budget_value
CONCAT '","gross_budget":"' CONCAT NEW.gross_budget CONCAT '"}' ,
0, CURRENT_TIMESTAMP , CURRENT_TIMESTAMP) ;
答案 0 :(得分:1)
你的触发器上几乎没有问题,我会用这样的东西来解决它:
CREATE TRIGGER trig1
AFTER INSERT ON table1
FOR EACH ROW
INSERT INTO table2 (entity_type,`data`,processed,created_at,last_updated_at)
VALUES (
'campaigns',
CONCAT(
'{',
CONCAT_WS(
',',
CONCAT('"campaign_id":"', NEW.campaign_id, '"'),
CONCAT('"name":"', NEW.name, '"'),
CONCAT('"status":"', NEW.status, '"'),
),
'}'
),
0,
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP
);
CONCAT将使用值和结束{
连结开头}
。
将使用CONCAT_WS计算所有值,这将创建逗号分隔的值字符串,并且它将忽略空值。
这将使触发器更具可读性。
答案 1 :(得分:1)
您在声明中错误地使用了CONCAT
。
应该如下面的陈述所示:
VALUES ( DEFAULT , 'campaigns',
CONCAT( '{"campaign_id":"', NEW.campaign_id,
'","name":"', NEW.name,
'","advertiser_id":"', NEW.advertiser_id,
'","start_date":"', NEW.start_date,
'","end_date":"', NEW.end_date,
'","status":"', NEW.status,
'","pacing":"', NEW.pacing,
'","budget_value":"', NEW.budget_value,
'","gross_budget":"', NEW.gross_budget,
'"}' ),
0, CURRENT_TIMESTAMP , CURRENT_TIMESTAMP ) ;
文档参考: