mysql - 如何在mysql函数中返回

标签 mysql function

嘿,我的 mysql 架构上有以下函数,该函数应该像这样工作:

  if (especie=mEspecie && variedad=mVariedad) then
         then return precio;   else
         return 0; 
  end if;

但不知何故它不起作用,有人可以帮助我吗?

这是我的函数,自 @vipin 回答以来我已更新:

  CREATE DEFINER=`root`@`localhost` FUNCTION `BUSCA_PRECIO_MATRIZ`(mEspecie int, mVariedad int) RETURNS int(11)
    READS SQL DATA
    DETERMINISTIC
BEGIN
declare precio, especie,variedad integer;
declare cur1 cursor for
select ifnull(valor,0),idconf_especie,idconf_variedad from cc_matriz_precios_facturacion_recibidor where idconf_especie=mEspecie and idconf_variedad=mVariedad;

open cur1;
    loop_cur : loop 
        fetch cur1 into precio,especie,variedad;
            if(especie = mEspecie) then
                if  (variedad = mVariedad) then
                    return variedad;
                    LEAVE loop_cur;
                else
                    return variedad;
                    leave loop_cur;
                end if;
            end if;
    end loop;
    return 0;
close cur1;
return 0;
END

最佳答案

在我的查询中,如果在检查条件后在 cc_matriz_precios_facturacion_recibidor 表中找到任何数据,则返回 true,否则返回 false

    CREATE FUNCTION `BUSCA_PRECIO_MATRIZ`(mEspecie int, mVariedad int) RETURNS int(11)
        READS SQL DATA
        DETERMINISTIC
    BEGIN
    declare precio, especie,variedad, flag int;    

    select valor,idconf_especie,idconf_variedad, 1 into precio, especie,variedad, flag 
    from cc_matriz_precios_facturacion_recibidor
    where idconf_especie=mEspecie and idconf_variedad=mVariedad;
    IF(flag) THEN 
         return 1; 
    ELSE 
         return 0;
    END IF;
    END

或者简而言之

CREATE FUNCTION `BUSCA_PRECIO_MATRIZ`(mEspecie int, mVariedad int) RETURNS int(11)
            READS SQL DATA
            DETERMINISTIC
        BEGIN
        declare precio, especie,variedad int;   
        DECLARE flag int default 0;

        select valor,idconf_especie,idconf_variedad, 1 into precio, especie,variedad, flag 
        from cc_matriz_precios_facturacion_recibidor
        where idconf_especie=mEspecie and idconf_variedad=mVariedad;
        return flag;
        END

关于mysql - 如何在mysql函数中返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36180898/

相关文章:

php - require_once函数

c - 在 C 中声明 void 函数

mysql - 此查询仍返回空标题字段?

mysql - sql查询连接四张表

python - 用于删除空格的正则表达式

c - C 函数内的奇怪执行

oracle - 使用 Oracle trunc() 的结果奇怪?

c++ - 用函数替换宏会导致 "signed/unsigned mismatch"警告

mysql - 为什么我无法从 JDBC 连接池 ping 我的数据库?

php - 登录页面检查来自两个不同表的用户名