tags:

views:

23

answers:

1

Hi, I have problem with MySql, i try write triggers for table but my knowlage abut it is nearly none. What i want to do? I want update some of record in table A when someone put something in table B. But to do that first i need count row with value 1 and -1 from table B. This is my 3 table

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 ;
A: 

Trigger should looks like this:

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;$$

It's not end in 100% because I need add second UPDATE for vote_comment_in_minus but it works ! But i have another question it's possible to change 'FOR EACH ROW' to for example FOR INSERT ROW?

sebastian