mysql/oracle存储的数学公式 [英] mysql/oracle stored math formula

查看:426
本文介绍了mysql/oracle存储的数学公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以从Oracle和/或MySQL中存储的字符串中应用数学公式吗?

is there any way apply math formula from stored string in Oracle and or MySQL?

col1 | col2 | formula
---------------------
 2   |  2   | col1*col2
 2   |  3   | col1+col2 

SELECT * from tbl

result:
col1 | col2 | formula
---------------------
 2   |  2   |   4
 2   |  3   |   5

每行另一个公式

推荐答案

我认为您的意思是希望数据库解析公式字符串.例如,对于Oracle,您可以

I think what you're saying is you want to have the database parse the formula string. For example, for Oracle you could

  • 在表中添加一列以包含结果
  • 运行一条更新语句,该语句将使用表中列的值和公式的文本

  • Add a column to the table to contain the result
  • Run an update statement which would call a PL/SQL function with the values of the columns in the table and the text of the formula

更新{table}设置公式_结果= fn_calc_result(col1,col2,公式_列);

update {table} set formula_result = fn_calc_result (col1, col2, formula_column);

PL/SQL函数将通过用这些列的实际值替换"col1"和"col2"等来创建一个字符串.只要公式始终一致,您就可以使用常规表达式来执行此操作.

The PL/SQL function would create a string by replacing the "col1" and "col2" and so forth with the actual values of those columns. You can do that with regular expresions, as long as the formulas are consistently written.

然后使用

execute immediate 'select '||{formula}||' from dual' into v_return;
return v_return;

计算结果并返回.

当然,您也可以编写自己的解析器.如果您决定采用这种方式,请不要忘记处理操作优先级,括号等.

Of course, you could also write your own parser. If you decide to go that way, don't forget to handle operation precedence, parentheses, and so forth.

这篇关于mysql/oracle存储的数学公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆