2013-03-20 18 views
0

我怎么能在现场名称中使用varables?使用变量字段名称触发(插入)

delimiter | 

CREATE TRIGGER update_expression_counter BEFORE INSERT ON remake_town_expressions 
FOR EACH ROW BEGIN 

SET @type := NEW.`type`; 
SET @user_id := NEW.user_id; 
SET @user_profile_id := NEW.user_profile_id; 
SET @country_iso := NEW.country_iso; 
SET @place_id := NEW.`place_id`; 
SET @city_id := NEW.`city_id`; 
SET @field := 'comment_cnt'; 

SELECT CASE @type 
WHEN 'comment' THEN 'comment_cnt' 
WHEN 'photo' THEN 'photo_cnt' 
WHEN 'video' THEN 'video_cnt' 
WHEN 'tag' THEN 'tag_cnt' 
WHEN 'checkin' THEN 'checkin_cnt' 
END 
INTO @field; 

INSERT INTO `remake_town_counter` (`user_id`,`user_profile_id`,`country_iso`,`city_id`,`place_id`, @field) 
VALUES (@user_id,@user_profile_id,@country_iso,@city_id,@place_id,1) ON DUPLICATE KEY UPDATE @[email protected]+1,`rank`=`rank`+1; 

END; 
| 

delimiter ; 

这个返回错误。如果@field使用`写入,则查询字段将是变量名称,而不是变量值;

我不能老是用后CONCAT:

PREPARE stmt FROM @query; 
EXECUTE stmt; 
DEALLOCATE PREPARE stmt; 

,因为这是动态SQL

我怎么能卖出这个问题? 谢谢!

只有一个
+0

什么是确切的错误信息? – Jocelyn 2013-03-20 16:52:30

+0

#1064,我不能老是用字段名没有'%FIELD_NAME%',如果写'@ field' - 一切都很好,但在结果查询 - INSERT INTO ...'place_id','@ field')..哪里变量是名称不值 – DedMorozzz 2013-03-20 16:55:19

回答

0

找到解决方法: 查询写入各个领域,需要场+1更新; 我的结果

SET @comment_cnt := CASE WHEN @type = 'comment' THEN 1 ELSE 0 END; 
SET @photo_cnt := CASE WHEN @type = 'photo' THEN 1 ELSE 0 END; 
SET @video_cnt := CASE WHEN @type = 'video' THEN 1 ELSE 0 END; 
SET @tag_cnt := CASE WHEN @type = 'tag' THEN 1 ELSE 0 END; 
SET @checkin_cnt := CASE WHEN @type = 'checkin' THEN 1 ELSE 0 END;  

-- update user count actions in the place 

INSERT INTO `remake_town_counter` (`user_id`,`user_profile_id`,`country_iso`,`city_id`,`place_id`, `comment_cnt`,`photo_cnt`,`video_cnt`,`checkin_cnt`,`tag_cnt`,`rank`) 
VALUES (@user_id,@user_profile_id,@country_iso,@city_id,@place_id,@comment_cnt,@photo_cnt,@video_cnt,@checkin_cnt,@tag_cnt,1) 
ON DUPLICATE KEY UPDATE 
`comment_cnt`=`comment_cnt`[email protected]_cnt,`photo_cnt`=`photo_cnt`[email protected]_cnt,`video_cnt`=`video_cnt`[email protected]_cnt,`checkin_cnt`=`checkin_cnt`[email protected]_cnt,`tag_cnt`=`tag_cnt`[email protected]_cnt,`rank`=`rank`+1; 
相关问题