gpt4 book ai didi

MySQL存储函数-追加字符串

转载 作者:行者123 更新时间:2023-11-29 06:08:25 26 4
gpt4 key购买 nike

我已经搜索过,但找不到任何可以满足我需要的东西。我想创建一个存储函数,它将从辅助字段收集数据并返回逗号分隔的项目列表作为返回字符串。我似乎找不到任何方法来获取我在函数中创建的变量并迭代记录集并将每个结果 append 到变量,以便我可以返回它..见下文:

BEGIN
DECLARE searchCat INT;
DECLARE searchProd INT;
DECLARE metas CHAR;
SET searchCat = cat;
SET searchProd = prod;
SELECT * FROM offer_metas WHERE category = searchCat AND offer_id = searchProd
gatherMeta: LOOP
metas = metas + "," + meta_option;
ITERATE gatherMeta;
END LOOP gatherMeta;
RETURN metas;
END

该函数不会保存,因为我的语法是“metas = metas + meta_option;”。我正在寻找的是将“meta_option”的当前字段错误 append 到当前变量“metas”的命令,以便我可以在最后返回完整列表。

有什么想法吗?

更新 - 解决方案

BEGIN
DECLARE metas VARCHAR (300);

SELECT GROUP_CONCAT(CONCAT(mn.title,'=',offer_metas.meta_option) ORDER BY mo.cat_seq ASC) INTO metas
FROM offer_metas
LEFT JOIN meta_options as mo ON mo.id = offer_metas.meta_option
LEFT JOIN meta_names AS mn ON mn.category = mo.category AND mn.seq = mo.cat_seq
WHERE offer_metas.category = searchCat
AND offer_metas.offer_id = searchProd
ORDER BY cat_seq ASC;

RETURN metas;
END

然后我刚刚将 SQL 查询更新为如下(1 是我的 PHP 中的报价类别并填充到查询中):

SELECT offers.*, s.short_name AS sponsorName, s.logo AS sponsorLogo, getMetas(1,offers.id) AS metas 
FROM offers
LEFT JOIN sponsors AS s ON s.id=offers.carrier
GROUP BY offers.id
ORDER BY end_date ASC

最佳答案

为什么不只是

SELECT GROUP_CONCAT(meta_option SEPARATOR ',')
FROM offer_metas
WHERE category = searchCat AND offer_id = searchProd;

关于MySQL存储函数-追加字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10388262/

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