DELIMITER $$

DROP FUNCTION IF EXISTS `value_of_bit_index_is_true`$$
/*计算某个数字的某些索引的位的值是否都为1,索引类似1,2,3,4*/
CREATE FUNCTION `value_of_bit_index_is_true`(`number` INT, `idxies` VARCHAR(50)) RETURNS INT(11)
BEGIN
/*将1,2,3,4,5,6这样的字符串转换位表*/
SET @result = 1;

SET @i = LENGTH(`idxies`) - LENGTH(REPLACE(`idxies`,',','')); -- 算出分隔符的总数
SET @left_str = `idxies`;
WHILE @i>0 -- 有多少个分隔符就循环多少遍
DO
SET @sub_str = SUBSTR(@left_str,1,INSTR(@left_str,',')-1); -- 得到分隔符前面的字符串
SET @left_str = SUBSTR(@left_str,LENGTH(@sub_str)+LENGTH(',')+1); -- 得到分隔符后面的字符串
SET @n = TRIM(@sub_str);
SET @r=`value_of_bit_index`(`number`, @n);
IF @r = 0 THEN
SET @result = 0;
END IF;

SET @i = @i - 1;
END WHILE;
SET @n = TRIM(@left_str);
SET @r=`value_of_bit_index`(`number`, @n);
IF @r = 0 THEN
SET @result = 0;
END IF;

RETURN @result;
END$$

DELIMITER ;

posted on 2018-09-13 17:53  袁晓平  阅读(283)  评论(0编辑  收藏  举报