2013-05-30 58 views
1

請參閱下面的SQL。我們需要在表格中保存STD公式,但無法評估這一點。如何評估MYSQL的varchar列中的公式表

DROP TEMPORARY TABLE IF EXISTS _std ; 
CREATE TEMPORARY TABLE _std(stdformula VARCHAR(4000), mu DECIMAL(18,9), newValue DECIMAL(18,9)); 

INSERT INTO _std VALUES('SQRT((POWER((5-mu), 2) + POWER((6-mu), 2) + POWER((7-mu), 2) + POWER((7-mu), 2))/4)',5+6+7+7/4,7); 

SET @temp=(SELECT REPLACE(stdformula,'mu',mu) FROM _std); 
SELECT @temp; 
-- @temp=> SQRT((POWER((5-19.750000000), 2) + POWER((6-19.750000000), 2) + POWER((7-19.750000000), 2) + POWER((7-19.750000000), 2))/2); 
-- which is valid statement 
SELECT SQRT((POWER((5-19.750000000), 2) + POWER((6-19.750000000), 2) + POWER((7-19.750000000), 2) + POWER((7-19.750000000), 2))/2); 

- 如何使用動態sql方法評估@temp? - 以下準備工作不

PREPARE StmtStd FROM @temp; 
EXECUTE StmtStd; 
DEALLOCATE PREPARE Stmtstd;* 

回答

0

你缺少@tempSELECT。嘗試

... 
SET @temp= CONCAT('SELECT ', (SELECT REPLACE(stdformula,'mu',mu) FROM _std), ' result'); 
-- SELECT @temp;  
PREPARE StmtStd FROM @temp; 
EXECUTE StmtStd; 

輸出:

|   RESULT | 
------------------- 
| 13.525438994724 | 

這裏是SQLFiddle演示

+0

由於peterm它的工作完美... – Anil