本文介绍了Mysql函数从查询中返回一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我想创建一个函数来计算一个使用查询的值,并且我有一个返回值的问题: 缩短了,我的查询是: CREATE FUNCTION func01(value1 INT,monto DECIMAL(10,2))RETURNS DECIMAL(10,2) BEGIN SET @var_name = 0; 从table where data_init = 1中选择@ var_name = if(value1 = 1,monto * table.divisa_dolar,table.monto * divisa_euro); 返回@var_nam; END 我得到一个SQL语法错误。 解决方案假设这些都是通用名称(表格不会是一个好的表名),问题是您无法使用==进行比较。您还缺少一些关键语法(DECLARE,SELECT INTO等)。 更改为: CREATE FUNCTION func01(value1 INT,monto DECIMAL(10,2)) RETURNS DECIMAL(10,2) DETERMINISTIC BEGIN DECLARE var_name DECIMAL(10,2); SET var_name = 0; SELECT if(value1 = 1,monto * divisa_dolar,monto * divisa_euro)INTO var_name FROM table WHERE data_init = 1; RETURN var_name; END MySQL比较函数和运算符 相关问题:单等于MYSQL 功能帮助: http://www.databasejournal.com/features /mysql/article.php/3569846/MySQL-Stored-Functions.htm i want to create a function which calculates a value using a query and I am having a problem returning the value:Shortened, my query is:CREATE FUNCTION func01(value1 INT , monto DECIMAL (10,2)) RETURNS DECIMAL(10,2)BEGINSET @var_name = 0;select @var_name=if(value1 = 1,monto * table.divisa_dolar,table.monto *divisa_euro) from table where data_init = 1;return @var_nam;ENDI get a SQL syntax error. 解决方案 Assuming these are all generic names (table will not be a good table name), the problem is you can't use == for comparison. You are also missing some key syntax (DECLARE, SELECT INTO, etc.).Change to this:CREATE FUNCTION func01(value1 INT , monto DECIMAL (10,2))RETURNS DECIMAL(10,2)DETERMINISTICBEGIN DECLARE var_name DECIMAL(10,2); SET var_name = 0; SELECT if(value1 = 1,monto *divisa_dolar,monto *divisa_euro) INTO var_name FROM table WHERE data_init = 1; RETURN var_name;ENDMySQL Comparison Functions and OperatorsRelated Question: Single Equals in MYSQLFunction Help: http://www.databasejournal.com/features/mysql/article.php/3569846/MySQL-Stored-Functions.htm 这篇关于Mysql函数从查询中返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-08 10:52