动态解析计算
【问题】
Let’s say I have given data as string:
+———-+
|Size |
+———-+
|15X10 |
|5X4 |
|3 |
|2X6X5 |
+———-+
I want to write this column as integer like this:
+———-+
|Size |
+———-+
|150 |
|20 |
|3 |
|60 |
+———-+
Some of them are multiply of three numbers, some of them just one number. I can split the string but cannot make MySQL to calculate the number. Thank you.
【回答】
对于任意的四则运算、带多重括号且多个操作数的运算,用 SQL 很难实现。可以在 SPL 中用动态结构化计算来解决,脚本如下:
A |
|
1 |
$select * from tb |
2 |
=A1.run(Size=eval(replace(Size,"X","*"))) |
A2:将字符 X 替换为乘法符号 *,对字符串动态解析为表达式并计算