我遇到了MySql的问题,我尝试为表写入触发器,但我的知识几乎没有。 我想做的事?当有人把某些东西放在表B中时,我想要更新表A中的一些记录。但是为了做到这一点,我首先需要从表B中计算值为1和-1的行。 这是我的3桌
CREATE TABLE IF NOT EXISTS `wp_comment_vote` (
`vote_id` int(11) NOT NULL AUTO_INCREMENT,
`comment_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`value` int(11) NOT NULL,
`meta_id` int(11) NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`vote_id`)
)
CREATE TABLE IF NOT EXISTS `wp_comments` (
`comment_ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`comment_post_ID` bigint(20) unsigned NOT NULL DEFAULT '0',,
`user_id` bigint(20) unsigned NOT NULL DEFAULT '0'
`comment_author` tinytext NOT NULL,
`comment_author_email` varchar(100) NOT NULL DEFAULT '',
`comment_author_url` varchar(200) NOT NULL DEFAULT '',
`comment_author_IP` varchar(100) NOT NULL DEFAULT '',
`comment_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`comment_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`comment_content` text NOT NULLN',
PRIMARY KEY (`comment_ID`)
)
CREATE TABLE IF NOT EXISTS `wp_commentmeta` (
`meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`comment_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`meta_key` varchar(255) DEFAULT NULL,
`meta_value` longtext,
PRIMARY KEY (`meta_id`),
KEY `comment_id` (`comment_id`),
KEY `meta_key` (`meta_key`)
)
delimiter //
CREATE TRIGGER countvotecomment AFTER UPDATE ON wp_comment_vote
FOR EACH ROW BEGIN
UPDATE wp_commentmeta
IF wp_commentmeta.meta_key = 'vote_comment_in_minus' THAN
SET wp_commentmeta.meta_value = (SELECT COUNT( value ) as glosy FROM wp_comment_vote WHERE comment_id=wp_commentmeta.comment_id AND value = '-1');
ELSEIF wp_commentmeta.meta_key = 'vote_comment_in_plus' THAN
SET wp_commentmeta.meta_value = (SELECT COUNT( value ) as glosy FROM wp_comment_vote WHERE comment_id=wp_commentmeta.comment_id AND value = '1')
END;
delimiter ;
答案 0 :(得分:0)
触发器应如下所示:
CREATE TRIGGER votecomment
AFTER INSERT ON wp_comment_vote
FOR EACH ROW
BEGIN
UPDATE wp_commentmeta SET meta_value = (SELECT COUNT(value) FROM `wp_comment_vote` WHERE comment_id = NEW.comment_id AND value = 1) WHERE meta_key = 'vote_comment_in_plus';
END;$$
它不是以100%结尾,因为我需要为vote_comment_in_minus添加第二个UPDATE,但它有效!