如何将位值设置为变量并将其用作语句条件?

时间:2016-08-28 08:20:27

标签: mysql sql

我有一个查询(触发器),如下所示:

BEGIN     
    IF (new.edited_id IS NULL) THEN
        IF (SELECT 1 FROM users WHERE id = new.author_id AND IFNULL((active & b'1000000' > 0), 0) < 1) THEN
            SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "you cannot ask anymore";
        ELSEIF (SELECT 1 FROM users WHERE id = new.author_id AND IFNULL((active & b'10000000' > 0), 0) < 1) THEN
            SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "you cannot answer anymore";
        END IF;
    ELSE 
        IF (SELECT 1 FROM users WHERE id = new.author_id AND IFNULL((active & b'100000000' > 0), 0) < 1) THEN
            SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "you cannot edit anymore";
        END IF;
END

如您所见,那些内部IF语句的条件几乎相同。那么我该如何改进呢?

我的意思是,制作这样的东西:

IF (IFNULL((@variable & b'1000000' > 0), 0) < 1) THEN
IF (IFNULL((@variable & b'10000000' > 0), 0) < 1) THEN
IF (IFNULL((@variable & b'100000000' > 0), 0) < 1) THEN

我该如何处理?

0 个答案:

没有答案