Why mysql is giving error “Not allowed to return a result set from a function”?

前端 未结 2 1477
情书的邮戳
情书的邮戳 2020-12-01 16:54

I am trying to create a MySQL function using phpMyAdmin and getting this error.

#1415 - Not allowed to return a result set from a function

相关标签:
2条回答
  • 2020-12-01 17:21

    that is because you are using SELECT queries whose output is not stored into variables or temporary inside FUNCTION which must. Function can return only one single value. So your code should be something like this:

    CREATE TABLE t1 AS SELECT left_id AS c1 FROM mlm_user_mst WHERE parent_id=a AND left_id>0;
    CREATE TABLE t2 AS SELECT right_id AS c2 FROM mlm_user_mst WHERE parent_id=a AND right_id>0;
    

    or

    SELECT left_id AS c1 INTO @c1 FROM mlm_user_mst WHERE parent_id=a AND left_id>0 LIMIT 1; 
    
    SELECT right_id AS c2 INTO @c2 FROM mlm_user_mst WHERE parent_id=a AND right_id>0 LIMIT 1;
    
    0 讨论(0)
  • 2020-12-01 17:24

    Because

    SELECT left_id AS c1 FROM mlm_user_mst WHERE parent_id=a AND left_id>0; 
    

    doesn't set the variable c1, it returns a set with a column named c1

    You want

    SELECT left_id INTO c1 FROM mlm_user_mst WHERE parent_id=a AND left_id>0; 
    

    Similarly for c2.

    0 讨论(0)
提交回复
热议问题