delimiter // 
CREATE  FUNCTION getpercent(dec1 char(20),dec2 char(20)) 
returns char(50) 
begin 
DECLARE Result char(50); 
SET Result = ''; 
if ifnull(dec1,0)=0 or ifnull(dec2,0)=0 then  
      Result='0'; 
elseif dec1>=dec2 then 
      Result='100'; 
else 
      Result = round(dec1 / dec2 * 100, 2); 
      if substr(Result,0,1)='.' then 
         Result =concat('0',Result); 
      end if; 
end if; 
Result =concat(Result,'%'); 
return (Result); 
end // 

解决方案 »

  1.   

    就是if语句快 提示 可我已经按要求做的啊 我觉得没错 可还是报错 无解请帮忙指点一二
    if ifnull(dec1,0)=0 or ifnull(dec2,0)=0 then   
      Result='0';  
    elseif dec1>=dec2 then  
      Result='100';  
    else  
      Result = round(dec1 / dec2 * 100, 2);  
      if substr(Result,0,1)='.' then  
      Result =concat('0',Result);  
      end if;  
    end if;  
    哪错了??啊
      

  2.   

     楼主为什么认为有错误? 如果MYSQL报错,那请把错误提示贴出以供分析。
      

  3.   

    Err] 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 '='0'; 
    elseif dec1>=dec2 then 
          Result='100'; 
    else 
          Result = rou' at line 7
      

  4.   

    Err] 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 '='0'; 
    elseif dec1>=dec2 then 
          Result='100'; 
    else 
          Result = rou' at line 7
      

  5.   

    Err] 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 '='0'; 
    elseif dec1>=dec2 then 
          Result='100'; 
    else 
          Result = rou' at line 7
      

  6.   

    mysql> delimiter //
    mysql> CREATE FUNCTION getpercent(dec1 int,dec2 int)
        -> returns char(50)
        -> begin
        -> DECLARE Result char(50);
        -> SET Result = '';
        -> if ifnull(dec1,0)=0 or ifnull(dec2,0)=0
        -> then return 0;
        -> elseif dec1>=dec2
        -> then return 100;
        -> else
        -> set Result = round(dec1 / dec2 * 100, 2);
        -> if substr(Result,0,1)='.'
        -> then return round(dec1 / dec2 * 100, 2);
        -> else
        -> return concat(Result,'%');
        -> end if;
        -> end if;
        -> end //
    Query OK, 0 rows affected (0.00 sec)mysql> delimiter ;
    mysql> select getpercent(NULL,0);
    +--------------------+
    | getpercent(NULL,0) |
    +--------------------+
    | 0                  |
    +--------------------+
    1 row in set (0.00 sec)mysql> select getpercent(0,0);
    +-----------------+
    | getpercent(0,0) |
    +-----------------+
    | 0               |
    +-----------------+
    1 row in set (0.00 sec)mysql> select getpercent(1,2);
    +-----------------+
    | getpercent(1,2) |
    +-----------------+
    | 50.00%          |
    +-----------------+
    1 row in set (0.00 sec)mysql> select getpercent(2,1);
    +-----------------+
    | getpercent(2,1) |
    +-----------------+
    | 100             |
    +-----------------+
    1 row in set (0.02 sec)
      

  7.   

    100 显示100% 可以把
    elseif dec1>=dec2 
    then return concat(100,'%');  改成这样.
      

  8.   

    delimiter //  
    CREATE FUNCTION getpercent(dec1 char(20),dec2 char(20))  
    returns char(50)  
    begin  
    DECLARE Result char(50);  
    SET Result = '';  
    if ifnull(dec1,0)=0 or ifnull(dec2,0)=0 then   
      Result='0';  
    elseif dec1>=dec2 then  
      set Result='100';  
    else  
      Result = round(dec1 / dec2 * 100, 2);  
      if substr(Result,0,1)='.' then  
      Result =concat('0',Result);  
      end if;  
    end if;  
    Result =concat(Result,'%');  
    return (Result);  
    end //  
      

  9.   

    Mablevi的差不多 梦醒十分全都是复制我内个哒
    只是有一处
    if substr(Result,0,1)='.'
    then return round(dec1 / dec2 * 100, 2);
    似乎应该是我说做的返回值
    if substr(Result,0,1)='.' then  
      Result =concat('0',Result);