为什么这个 MySQL 函数返回 null?
描述: 实际运行的查询返回了 4 个结果,如下所示, 我所做的只是连接项目然后返回, 但出乎意料的是,它是空的。
我认为代码是不言自明的:
DELIMITER |
DROP FUNCTION IF EXISTS get_idiscussion_ask|
CREATE FUNCTION get_idiscussion_ask(iask_id INT UNSIGNED) RETURNS TEXT DETERMINISTIC
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE body varchar(600);
DECLARE created DATETIME;
DECLARE anonymous TINYINT(1);
DECLARE screen_name varchar(64);
DECLARE result TEXT;
DECLARE cur1 CURSOR FOR SELECT body,created,anonymous,screen_name from idiscussion left join users on idiscussion.uid=users.id where idiscussion.iask_id=iask_id;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
SET result = '';
OPEN cur1;
REPEAT
FETCH cur1 INTO body, created, anonymous, screen_name;
SET result = CONCAT(result,'<comment><body><![CDATA[',body,']]></body>','<replier>',if(screen_name is not null and !anonymous,screen_name,''),'</replier>','<created>',created,'</created></comment>');
UNTIL done END REPEAT;
CLOSE cur1;
RETURN result;
END |
DELIMITER ;
mysql> DELIMITER ;
mysql> select get_idiscussion_ask(1);
+------------------------+
| get_idiscussion_ask(1) |
+------------------------+
| NULL |
+------------------------+
1 row in set (0.01 sec)
mysql> SELECT body,created,anonymous,screen_name from idiscussion left join users on idiscussion.uid=users.id where idiscussion.iask_id=1;
+------+---------------------+-----------+-------------+
| body | created | anonymous | screen_name |
+------+---------------------+-----------+-------------+
| haha | 2009-05-27 04:57:51 | 0 | NULL |
| haha | 2009-05-27 04:57:52 | 0 | NULL |
| haha | 2009-05-27 04:57:52 | 0 | NULL |
| haha | 2009-05-27 04:57:53 | 0 | NULL |
+------+---------------------+-----------+-------------+
4 rows in set (0.00 sec)
对于那些认为代码不言自明的人:
为什么函数返回NULL
?
Description:
the query actually run have 4 results returned,as can be see from below,
what I did is just concate the items then return,
but unexpectedly,it's null.
I think the code is self-explanatory:
DELIMITER |
DROP FUNCTION IF EXISTS get_idiscussion_ask|
CREATE FUNCTION get_idiscussion_ask(iask_id INT UNSIGNED) RETURNS TEXT DETERMINISTIC
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE body varchar(600);
DECLARE created DATETIME;
DECLARE anonymous TINYINT(1);
DECLARE screen_name varchar(64);
DECLARE result TEXT;
DECLARE cur1 CURSOR FOR SELECT body,created,anonymous,screen_name from idiscussion left join users on idiscussion.uid=users.id where idiscussion.iask_id=iask_id;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
SET result = '';
OPEN cur1;
REPEAT
FETCH cur1 INTO body, created, anonymous, screen_name;
SET result = CONCAT(result,'<comment><body><![CDATA[',body,']]></body>','<replier>',if(screen_name is not null and !anonymous,screen_name,''),'</replier>','<created>',created,'</created></comment>');
UNTIL done END REPEAT;
CLOSE cur1;
RETURN result;
END |
DELIMITER ;
mysql> DELIMITER ;
mysql> select get_idiscussion_ask(1);
+------------------------+
| get_idiscussion_ask(1) |
+------------------------+
| NULL |
+------------------------+
1 row in set (0.01 sec)
mysql> SELECT body,created,anonymous,screen_name from idiscussion left join users on idiscussion.uid=users.id where idiscussion.iask_id=1;
+------+---------------------+-----------+-------------+
| body | created | anonymous | screen_name |
+------+---------------------+-----------+-------------+
| haha | 2009-05-27 04:57:51 | 0 | NULL |
| haha | 2009-05-27 04:57:52 | 0 | NULL |
| haha | 2009-05-27 04:57:52 | 0 | NULL |
| haha | 2009-05-27 04:57:53 | 0 | NULL |
+------+---------------------+-----------+-------------+
4 rows in set (0.00 sec)
For those who don't think the code is self-explanatory:
Why the function returns NULL
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
重命名您的变量和输入参数,它们是不明确的。
此查询:
返回您之前声明的变量(为 NULL),而不是表列。
在变量名称和输入参数名称前面添加下划线。
此外,您还对结果进行了额外的分配:
在
FETCH
失败后,处理程序设置done
,但result
仍然被分配。将处理程序更改为:
最后:在 MySQL 中,这可以通过单个查询来完成。 不需要用函数来做到这一点。
Rename your variables and the input parameter, they're ambiguous.
This query:
returns your previously declared variables (which are
NULL
), not the table columns.Prepend the variable names and the input parameter name with an underscore.
Also you make an extra assignment to result:
The handler sets
done
after theFETCH
fails, but theresult
gets assigned nevertheless.Change your handler to:
Finally: in
MySQL
, this can be done with a single query. There is no need to do it with a function.请记住,将任何字符串与 NULL 连接起来都会返回 NULL。 尝试这个测试:
这将返回 NULL。
因此,当您循环游标时,如果任何行上的
body
或created
列为 NULL,则结果
也将变为 NULL。 然后,在循环的后续迭代中,与 NULL结果
连接的任何内容都无效; 它保持为 NULL。尝试这样的操作:
COALESCE()
函数是标准 SQL 中的一个有用函数。 它返回第一个非 NULL 参数。Keep in mind that concatenating any string together with a NULL returns NULL. Try this test:
This returns NULL.
So as you loop through your cursor, if the
body
orcreated
columns are NULL on any row, theresult
becomes NULL. Then on subsequent iterations of the loop anything concatenated with a NULLresult
has no effect; it stays NULL.Try something like this:
The
COALESCE()
function is a useful function in standard SQL. It returns its first non-NULL argument.CONCAT_WS(分隔符,str1,str2,...)
CONCAT_WS() 代表 CONCAT With Separator,是 CONCAT() 的特殊形式。 第一个参数是其余参数的分隔符。 分隔符添加在要连接的字符串之间:分隔符可以是字符串,其余参数也可以是字符串。 如果分隔符为 NULL,则结果为 NULL。 该函数会跳过分隔符参数后面的任何 NULL 值。
mysql>
mysql>
在 MySQL 4.0.14 之前,CONCAT_WS() 会跳过空字符串和 NULL 值。
CONCAT_WS(separator, str1, str2,...)
CONCAT_WS() stands for CONCAT With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated: The separator can be a string as can the rest of the arguments. If the separator is NULL, the result is NULL. The function skips any NULL values after the separator argument.
mysql>
mysql>
Before MySQL 4.0.14, CONCAT_WS() skips empty strings as well as NULL values.
我尝试说得很详细,因为您的问题不是;)
您期望函数的返回值是非 NULL,因为您通过仅连接非 NULL 字符串来创建返回值。
仅当其中一个字符串为 NULL 时,整个返回值才会为 NULL。 您的演示数据仅在 screen_name 中包含 NULL 值,但您尊重这种情况。
但不知何故(atm,我不知道如何)其中一个值必须为 NULL,并且要查看的相关行是带有大 CONCAT 的行。
如果出于调试原因将相关行缩短为:
它是否仍返回 NULL?
I try to be verbose because your question isn't ;)
You're expecting the return value of the function to be non-NULL since you're creating the return value by concatenating only non-NULL strings.
Only if one of the strings was NULL, the whole return value would be NULL. Your demo data only contains NULL values in screen_name, but you respected that case.
But somehow (atm I have no idea how) one of the values must be NULL and the relevant line to look at ist the one with the big CONCAT.
What if you shorten the relevant line for debug reasons to:
Does it still return NULL?