我需要在Mysql中创建一个应该执行以下操作的函数:
按以下方式从表中选择3列值:
SELECT column1,column2,column3 FROM table WHERE id = value;
对这些列的值进行排序,以便了解最大,中间和最小。
我的问题是双重的:
1)有没有办法获得中间值,因为有GREATEST()和LEAST()?
2)为什么以下函数总是返回以下错误:您的SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在'2)附近使用正确的语法; END $$ DELIMITER'在第27行
DELIMITER $$
CREATE FUNCTION abc_value(partita INT)
RETURNS DOUBLE DETERMINISTIC
BEGIN
DECLARE a, b, c, column1, column2, column3 DOUBLE;
DECLARE a CURSOR FOR SELECT GREATEST(column1, column2, column3) FROM table where id = value;
DECLARE c CURSOR FOR SELECT LEAST(column1, column2, column3) FROM table where id = value;
DECLARE column1 CURSOR FOR SELECT column1 FROM table where id = value;
DECLARE quotax CURSOR FOR SELECT column2 FROM table where id = value;
DECLARE quota2 CURSOR FOR SELECT column3 FROM table where id = value;
IF column1<> a THEN
IF column1<> b THEN
SET c = column1;
END IF;
END IF;
IF column2<> a THEN
IF column2<> b THEN
SET c = column2;
END IF;
END IF;
IF column3<> a THEN
IF column3<> b THEN
SET c = column3;
END IF;
END IF;
RETURN ROUND((a+b-c)/3),2);
END$$
DELIMITER;
答案 0 :(得分:0)
1)如果您有3个项目并且想要选择中间项目,请执行以下操作:
SELECT
value
FROM
table
ORDER BY
value
LIMIT 1,1 --index, amount (index starts from 0, so 1 is the second record)