MySQL将字符串计算为数学表达式

时间:2015-07-30 21:45:53

标签: mysql

目前,我正在使用此功能:

DELIMITER //
CREATE FUNCTION SUM_OF_LIST(s TEXT)
  RETURNS DOUBLE
  DETERMINISTIC
  NO SQL
BEGIN
  DECLARE res DOUBLE DEFAULT 0;
  WHILE INSTR(s, ",") > 0 DO
    SET res = res + SUBSTRING_INDEX(s, ",", 1);
    SET s = MID(s, INSTR(s, ",") + 1);
  END WHILE;
  RETURN res + s;
END //
DELIMITER ;

@Found here: Summing a comma separated column in MySQL 4 (not 5)

总结这样的MySQL结果:

30.48, 7.41, 5.01, 18.01, 11.07, 9.12, 45.90, 11.56, 37.62, 23.34, 2.23, 8.46, 49.20, 7.82, 7.11, 11.22, 581.79, 15.42, 22.68, 15.06, 115.20, 4.82, 69.30, 77.00, 6.90, 3.92, 43.26, 5.63, 5.40, 9.08, 6.96, 10.54, 4.35, 29.76, 32.79, 20.16, 26.22, 9.81, 11.86, 6.34, 10.28, 38.08, 35.25, 58.95, 6.32, 50.92, 31.04, 58.22, 22.23, 22.52, 18.46, 8.09, 8.30, 36.54, 14.55, 4.35, 9.92, 4.45, 32.46, 55.92, 61.02, 26.40, 4.05, 6.36, 3.38, 20.61, 19.02, 9.30, 9.76, 45.42, 11.79, 14.45, 14.88, 24.38, 12.69, 28.80, 38.10, 31.86, 16.47, 46.74, 70.80, 6.27, 8.20, 16.47, 61.80, 61.80, 7.70, 14.20, 19.83, 19.64, 19.62, 81.60, 56.52, 56.70, 28.16, 1.71, 16.58, 6.09, 11.90, 71.44, 174.45, 5.89, 9.23, 8.75, 15.12, 7.40, 21.78, 8.04, 10.86, 19.02, 8.76, 76.23, 7.06, 19.80, 65.52, 16.12, 9.99, 11.41, 10.29, 9.45, 56.64, 12.90, 24.30, 74.29, 33.62, 13.50, 9.71, 9.60, 69.30, 19.25, 28.16, 27.86, 9.16, 25.80, 30.84, 10.58, 68.04, 5.03, 19.50, 70.20, 8.60, 20.22, 126.27, 23.01, 92.40, 37.26, 19.62, 78.48, 74.46, 11.73, 88.40, 25.30, 8.13, 181.44, 31.23, 9.12, 22.23, 50.92, 3.69, 4.06, 8.75, 8.75, 13.40, 14.04, 14.52, 26.64, 79.74, 8.96, 16.20, 15.20, 26.61, 9.45, 42.60, 21.84, 565.74, 108.24, 18.80, 13.56, 5.97, 16.47, 61.45, 8.00, 28.40, 34.92, 33.75, 5.04, 24.75, 13.00, 78.00, 9.90, 14.92, 11.16, 12.16, 32.04, 9.81, 7.98, 36.84, 15.20, 7.09, 6.86, 31.32, 20.37, 6.30, 186.30, 7.09, 4.50, 4.93, 25.68, 2.25, 13.26, 29.02, 17.90, 11.10, 5.01, 7.41, 42.60, 15.42, 13.50, 33.00, 70.80, 6.38, 9.93, 7.58, 11.76, 35.84, 12.60, 17.20, 17.20, 74.29, 21.84, 3.15, 554.73, 216.00, 27.14, 12.65, 29.22, 33.66, 7.98, 7.69, 9.84, 8.10, 5.48, 21.96, 74.76, 30.30, 118.68, 3.78, 19.32, 17.40, 20.16, 10.77, 21.54, 12.13, 11.68, 16.92, 16.47, 7.01, 3.72, 20.58, 100.75, 4.46, 20.84, 14.42, 18.96, 8.50, 10.30, 11.73, 6.70, 9.12, 31.32, 94.38, 17.91, 7.70, 18.81, 16.08, 4.54, 14.82, 19.86, 47.52, 3.94, 50.92, 3.07, 9.23, 9.23, 8.75, 15.84, 2.25, 7.41, 16.08, 3936.00, 32.22, 11.82, 8.46, 33.93, 15.12, 27.37, 9.12, 16.81, 50.43, 568.26, 15.42, 446.49, 25.29, 7.50, 3.92, 7.08, 23.82, 18.66, 33.60, 9.68, 18.81, 20.46, 14.86, 4.39, 16.47, 16.93, 30.48, 6.87, 145.80, 147.75, 47.11, 4.56, 15.40, 71.15, 16.92, 30.54, 10.30, 10.92, 5.85, 9.81, 57.84, 55.68, 1507.20, 6.70, 10.30, 7.68, 24.00, 27.48, 7.58, 19.25, 20.58, 4.70, 8.06, 19.03, 9.23, 9.23, 9.30, 17.74, 11.85, 22.25, 5.42, 3.40, 53.76, 9.36, 15.33, 15.30, 53.76, 8.46, 15.60, 7.58, 83.93, 15.14, 61.52, 7.58, 6.88, 7.57, 14.36, 30.27, 229.50, 121.77, 13.53, 5.47, 32.61, 7.70, 100.62, 30.54, 30.81, 4.50, 19.96, 5.06, 36.84, 51.24, 3.33, 7.80, 14.84, 8.06, 15.36, 6.88, 17.70, 10.18, 26.08, 10.74, 13.53, 17.79, 78.48, 12.36, 22.80, 26.04, 37.62, 11.22

这个功能有效,但我希望总结大量数据,在我的测试中,我可以在~30个左右的时间内评估大约12个Mi数学运算。

但是当它在规范化的数据库中运行时,我可以在< 0.5段。

可以将像"10.02 + 1.03 + 4.01 + 5.07"这样的字符串作为数学表达式进行评估吗?

我已经尝试使用CAST eval,但eval不会返回任何内容:(

0 个答案:

没有答案