计算mysql触发器

时间:2015-02-07 06:54:33

标签: php mysql triggers

现在我在mysql触发器中面临一些问题,因为我很陌生。

我有两个名为app_formvenue_master的表。问题是,当我插入表app_form时,我必须从venue表中计算venue_master并使用该值更新app_form

现在在计算场地时,我必须从场地主人获取数据并将其放入while循环(如mysql_querymysql_fetch_assoc概念)并比较一些值然后最终获得场地。

那么语法是什么?什么是逻辑?

请帮忙......

经过大量研究,我编写了一些代码。请检查是否正常。我在哪里遇到逻辑问题?



DELIMITER $$
DROP TRIGGER IF EXISTS before_form_insert $$
CREATE TRIGGER before_form_insert
BEFORE INSERT ON `app_form`
FOR EACH ROW

BEGIN

LOCK TABLES app_form WRITE;
DECLARE done INT DEFAULT FALSE;
DECLARE vId, vCap, cnt, cntTotal, admit_no, venue, venueName INT;
DECLARE vName, r1, r2, roll VARCHAR(250);
DECLARE cur1 CURSOR FOR SELECT `venue_id` , `venue_capacity` FROM venue_master ORDER BY venue_id ASC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN cur1;

read_loop: LOOP
FETCH cur1 INTO vId, vCap;
IF done THEN
LEAVE read_loop;
END IF;
SET cnt = ( SELECT count(*) FROM app_form WHERE status=1 AND venuename=vId );
IF cnt < vCap THEN
SET cntTotal = ( SELECT count(*) FROM app_form WHERE status=1 );
SET admit_no = (cntTotal + 1);
SELECT venue_name INTO vName FROM venue_master WHERE venue_id = vId;
SET venue = vId;
SET venueName = vName;
SET r1=UPPER(SUBSTRING(vName,1,4));
SET r2=LPAD(admit_no, 5, 0);
SET roll=CONCAT(r1,'2015',r2);
LEAVE read_loop;
END IF;
END LOOP;

CLOSE cur1;

SET NEW.admit_no=admit_no, NEW.roll_no=roll, NEW.venuename=venue, NEW.venue_name=venueName;

UNLOCK TABLES;

END $$;
DELIMITER ;
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

如果您只需要计算您的牌桌包含的场地数量。您可以添加计数查询并将$ value插入到您的app_form值中。

$value =  mysql_query('select count from venue') ; 


or if you need to get some value from the venue 

$query =  mysql_query('select *  count from venue') ; 

while ($r = mysql_fetch_array($query )){

   if($r['somevalue'] = $othervalue) {
      //perform your operation // eg insert ur app-form data 
   }

}

我刚刚告诉你逻辑语法错误所以请修复