1

我试图在 IF 语句中给出两个条件......下面的两个触发器没有显示任何语法错误,但它们也不起作用。

触发器:1:

如果存在则删除触发器forumpostTrigger

CREATE DEFINER = `livecomm`@`localhost` TRIGGER `forumpostTrigger` AFTER INSERT ON `forum_post`
FOR EACH
ROW BEGIN if( (

SELECT count( * )
FROM forum_post
WHERE uid = new.uid
AND parent_id IS NOT NULL
AND is_answer_accepted ) >=30
)
THEN if( (

SELECT sum( vote_count )
FROM forum_post
WHERE uid = new.uid
AND parent_id IS NOT NULL
AND is_answer_accepted ) >=15
)
THEN
INSERT INTO user_badges( uid, bedge_id, is_active )
VALUES (
new.uid, '10', '1'
);

DELETE FROM user_badges WHERE uid = new.uid AND badge_id =9;

END IF ;

END IF ;

END

触发器:2:

DROP TRIGGER IF EXISTS `forumpostTrigger` ;

CREATE DEFINER = `livecomm`@`localhost` TRIGGER `forumpostTrigger` AFTER INSERT ON `forum_post`
FOR EACH
ROW BEGIN if( (
(

SELECT count( * )
FROM forum_post
WHERE uid = new.uid
AND parent_id IS NOT NULL
AND is_answer_accepted ) >=30
) && (
(

SELECT sum( vote_count )
FROM forum_post
WHERE uid = new.uid
AND parent_id IS NOT NULL
AND is_answer_accepted
) >=15
)
)
THEN
INSERT INTO user_badges( uid, bedge_id, is_active )
VALUES (
new.uid, '10', '1'
);

DELETE FROM user_badges WHERE uid = new.uid AND badge_id =9;

END IF ;

END 

如何为 if 语句提供多个条件,例如: if ((condition) && (condition))

4

2 回答 2

2
DROP TRIGGER IF EXISTS `forumpostTrigger` ;

CREATE DEFINER = `livecomm`@`localhost` TRIGGER `forumpostTrigger` AFTER INSERT ON `forum_post`
FOR EACH
ROW BEGIN if( (

SELECT count( * )
FROM forum_post
WHERE uid = new.uid
AND parent_id IS NOT NULL
AND is_answer_accepted ) =3
)
THEN
INSERT INTO user_badges( uid, badge_id, is_active )
VALUES (
new.uid, '9', '1'
);

END IF ;

if( (
SELECT count( * )
FROM forum_post
WHERE uid = new.uid
AND parent_id IS NOT NULL
AND is_answer_accepted ) =30
)
THEN if( (

SELECT sum( vote_count )
FROM forum_post
WHERE uid = new.uid
AND parent_id IS NOT NULL
AND is_answer_accepted ) >=15
)
THEN
INSERT INTO user_badges( uid, badge_id, is_active )
VALUES (
new.uid, '10', '1'
);

DELETE FROM user_badges WHERE uid = new.uid AND badge_id =9;

END IF ;

END IF ;

END 
于 2013-03-15T15:13:39.757 回答
0

您可以尝试使用此语法

IF condition1
IF condition2
BEGIN
..
END
于 2013-03-15T14:36:31.890 回答