0

只是为了测试,

但很奇怪:

    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)
4

4 回答 4

1

你应该做

Select Result
于 2009-05-27T00:27:16.377 回答
1

1)你没有在你的例子中调用你的函数。

2) 连接任何与 null 结果为 null

于 2009-05-27T00:27:21.197 回答
0

如果你改变这个:

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 cur1 CURSOR FOR SELECT   
ifnull(body,'')  
,ifnull(created,now())    
,ifnull(anonymous,0)  
,ifnull(screen_name,'')  
FROM idiscussion   
LEFT JOIN users on idiscussion.uid=users.id   
WHERE idiscussion.iask_id=iask_id;

CONCATing nulls 应该没有任何问题

另外我想知道这部分
where idiscussion.iask_id=iask_id;
是否不会产生名称冲突,也许您应该将参数
iask_id 更改为
piask_id
(但我不确定那部分)。

于 2011-03-09T22:58:03.080 回答
0

你......连接一个空值和一个字符串......这是一个空值。如果您甚至不调用它,存储过程与它有什么关系?

于 2009-05-27T00:28:50.070 回答