gpt4 book ai didi

mysql - 当表没有任何行时,程序给出错误

转载 作者:行者123 更新时间:2023-11-30 00:07:56 24 4
gpt4 key购买 nike

我有以下数据库表:表名称 - dim_module

id       Creation_Date         Goals  Alternative    Value
-----------------------------------------------------------
1 2014-04-17 10:09:30 G1 A 0.86
2 2014-04-17 10:09:30 G1 B 0.87
3 2014-04-17 10:09:30 G2 A 0.5
4 2014-04-17 10:09:30 G2 B 0

我正在使用以下过程来获得所需的输出

CREATE DEFINER=`root`@`localhost` PROCEDURE `stmt`()
BEGIN
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(goals = ''',
goals,
''', round(value, 2), NULL)) AS ',
goals
)
) INTO @sql
FROM sgwebdb.dim_module;
SET @sql = CONCAT('SELECT alternative, ', @sql, ' FROM sgwebdb.dim_module GROUP BY alternative');

prepare stmt from @sql;

但是,如果 Dim_Module 没有任何行,则程序不会被处理,并且我会收到错误。

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1

请帮忙清理空 table 。

最佳答案

也许是因为 @sql 为空。

尝试使用 IFNULL 将其设置为某个值

SET @sql = CONCAT('SELECT alternative, ', IFNULL(@sql,'somecolumnor*here'), ' FROM sgwebdb.dim_module GROUP BY alternative');

或者检测@sql为空并执行其他操作。

关于mysql - 当表没有任何行时,程序给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24358588/

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