我在Mysql中有CONV函数:
I have the CONV function in Mysql:
mysql> CONV(26,10,26)
-> 10
但我想要这样的东西:
mysql> CONV_LETTERS(26,10,26)
-> aa
如何在 MySQL 中将基数从 10 转换为基数 26,但只能使用字母以使数字 26 变为 aa?
更新
到目前为止:
delimiter //
CREATE PROCEDURE `base10_to_base26_letters`(IN `nr` BIGINT)
BEGIN
SET @letters='abcdefghijklmnopqrstuvwxyz';
select SUBSTRING(@letters,`nr`+1,1);
END//
更新 2
试图将 this php 函数转换为 MySQL 过程.
Trying to convert this php function to MySQL procedure.
PHP函数:
function generateAlphabet($na) {
$sa = "";
while ($na >= 0) {
$sa = chr($na % 26 + 65) . $sa;
$na = floor($na / 26) - 1;
}
return $sa;
}
到目前为止我的 MySQL 程序:
My MySQL procedure so far:
delimiter //
CREATE PROCEDURE `base10_to_base26_letters`(IN `nr` BIGINT)
BEGIN
SET @n=`nr`,@letters='abcdefghijklmnopqrstuvwxyz',@r='';
while @n>=0 do
set @n=@n/26-1,@r=concat(SUBSTRING(@letters,@n%26,1),@r);
end while;
select @r;
END//
为什么我尝试的任何数字都只能得到 z?
Why I only get z for any number I tried?
此存储函数将单个 int 转换为 base26:
This stored function converts a single int to base26:
DROP FUNCTION IF EXISTS `base10to26`;
DELIMITER ;;
CREATE FUNCTION `base10to26`(`theNumber` INT(11) UNSIGNED)
RETURNS VARCHAR(20) READS SQL DATA
BEGIN
DECLARE colTxt VARCHAR(20);
DECLARE value INT(11);
SET colTxt = '';
REPEAT
SET value = theNumber % 26;
IF value = 0 THEN SET value = 26; END IF;
SET theNumber = (theNumber - value) / 26;
SET colTxt = CONCAT(CHAR(value+64), colTxt);
UNTIL theNumber = 0 END REPEAT;
RETURN colTxt;
END;;
DELIMITER ;
这篇关于从基数 10 到基数 26 只用字母,所以 26 将是 aa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!