gpt4 book ai didi

mysql - MySQL函数中关联数组作为用户变量

转载 作者:行者123 更新时间:2023-11-29 13:35:20 24 4
gpt4 key购买 nike

是否可以将关联数组定义为MySQL函数内的用户变量?如果可能的话,我如何创建一个数组,例如:

nep_years[2002]=[31,31,32,32,31,30,30,29,30,29,30,30];

在 MySQL 函数内部?

非常感谢您的帮助。

最佳答案

Mysql 中没有数组,但您可以使用临时表和函数来实现类似的操作。

不确定这是否是您想要的。

#Delete temp table and functions
DROP TABLE IF EXISTS nep_years_vals;
DROP FUNCTION IF EXISTS `nep_func`;
DROP FUNCTION IF EXISTS `nep_func2`;

#create new temp table for 'array'
CREATE TEMPORARY TABLE nep_years_vals(nep_key INT, nep_val INT);
#Insert array values
INSERT INTO nep_years_vals(nep_key, nep_val) VALUES ('2002', '31');
INSERT INTO nep_years_vals(nep_key, nep_val) VALUES ('2002', '31');
INSERT INTO nep_years_vals(nep_key, nep_val) VALUES ('2002', '32');
INSERT INTO nep_years_vals(nep_key, nep_val) VALUES ('2002', '32');
INSERT INTO nep_years_vals(nep_key, nep_val) VALUES ('2002', '31');
INSERT INTO nep_years_vals(nep_key, nep_val) VALUES ('2002', '30');
INSERT INTO nep_years_vals(nep_key, nep_val) VALUES ('2002', '30');
INSERT INTO nep_years_vals(nep_key, nep_val) VALUES ('2002', '29');
INSERT INTO nep_years_vals(nep_key, nep_val) VALUES ('2002', '30');
INSERT INTO nep_years_vals(nep_key, nep_val) VALUES ('2002', '29');
INSERT INTO nep_years_vals(nep_key, nep_val) VALUES ('2002', '30');
INSERT INTO nep_years_vals(nep_key, nep_val) VALUES ('2002', '30');
#Declare function for selecting array element
DELIMITER $$
CREATE FUNCTION `nep_func`(`nep_key_search` INT, `nep_element_num` INT)
RETURNS INT
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE myid INT UNSIGNED;
select nep_val INTO myid from nep_years_vals WHERE nep_key = nep_key_search LIMIT nep_element_num, 1;
RETURN myid;
END;
$$
#Declare function for getting multiple elements from one array
DELIMITER $$
CREATE DEFINER=`root`@`localhost` FUNCTION `nep_func2`(`nep_key_search` INT, `nep_elements` TEXT)
RETURNS text
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE inipos INTEGER;
DECLARE endpos INTEGER;
DECLARE maxlen INTEGER;
DECLARE item VARCHAR(100);
DECLARE delim VARCHAR(1);
DECLARE allitems TEXT DEFAULT '';
DECLARE temp INTEGER;
DECLARE temp2 INTEGER DEFAULT NULL;

SET delim = ',';
SET inipos = 1;
SET nep_elements = CONCAT(nep_elements, delim);
SET maxlen = LENGTH(nep_elements);

REPEAT
SET endpos = LOCATE(delim, nep_elements, inipos);
SET item = SUBSTR(nep_elements, inipos, endpos - inipos);

IF item <> '' AND item IS NOT NULL THEN
IF allitems <> '' THEN
SET allitems = CONCAT(allitems, ',');
END IF;
SET temp2 = nep_func(nep_key_search, CONVERT(item, UNSIGNED INTEGER));
IF temp2 IS NULL THEN
SET allitems = CONCAT(allitems, 'NULL');
END IF;
IF temp2 IS NOT NULL THEN
SET allitems = CONCAT(allitems, temp2);
END IF;
#SET allitems = CONCAT(allitems, nep_func(nep_key_search, CONVERT(item, UNSIGNED INTEGER)));
END IF;
SET inipos = endpos + 1;
UNTIL inipos >= maxlen END REPEAT;

RETURN allitems;
END;
$$

#Examples, tested on mysql 5.5.27 WIN32
select nep_func(2002, 0);
#Result INTEGER: 31
select nep_func2(2002, '0,1,2,3,100,101');
#Result TEXT: 31,31,32,32,NULL,NULL
#If element does not exists it returns "NULL"

字符串分割借自 here

关于mysql - MySQL函数中关联数组作为用户变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18823270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com