根据 case when 子句的条件执行不同的准备语句
Executing different prepared statement based on conditions of case when clause
我是 MySQL 的新手,我正在尝试根据 case when 子句的条件执行不同的准备语句。问题是找到 table STATION 的 LAT_N 列的中位数,我想在 LAT_N 记录的偶数和奇数时使用 Case。
这是我为奇数情况准备的声明:
SET @foo = (SELECT ROUND((COUNT(*)-1)/2) FROM STATION);
PREPARE STMT1 FROM 'SELECT ROUND(LAT_N,4) FROM STATION ORDER BY LAT_N LIMIT ?,1';
EXECUTE STMT1 USING @foo;
这是偶数:
SET @foo1 = (SELECT ROUND(COUNT(*)/2) FROM STATION);
SET @foo2 = (SELECT ROUND((COUNT(*)+2)/2) FROM STATION);
PREPARE STMT2 FROM 'SELECT ROUND((((SELECT ROUND(LAT_N,4) FROM STATION ORDER BY LAT_N LIMIT ?,1) + (SELECT ROUND(LAT_N,4) FROM STATION ORDER BY LAT_N LIMIT ?,1))/2),4)';
EXECUTE STMT2 USING @foo1, @foo2;
而我的想法和这段代码有点相似:
SELECT CASE WHEN COUNT(*)%2=0 THEN EXECUTE STMT2 USING @foo1, @foo2
ELSE EXECUTE STMT1 USING @foo
FROM STATION
END;
但是那个代码 returns 错误 1064 (42000) 所以我想知道是否有任何替代方法。
非常感谢
我找到了这个问题的答案,其中包括程序的使用
DELIMITER $$
CREATE PROCEDURE latmed()
BEGIN
IF (SELECT COUNT(*) FROM STATION)%2=0 THEN
SET @foo1 = (SELECT ROUND(COUNT(*)/2) FROM STATION);
SET @foo2 = (SELECT ROUND((COUNT(*)+2)/2) FROM STATION);
PREPARE STMT2 FROM 'SELECT ROUND((((SELECT ROUND(LAT_N,4) FROM STATION ORDER BY LAT_N LIMIT ?,1) + (SELECT ROUND(LAT_N,4) FROM STATION ORDER BY LAT_N LIMIT ?,1))/2),4)';
EXECUTE STMT2 USING @foo1, @foo2;
ELSE
SET @foo = (SELECT ROUND((COUNT(*)-1)/2) FROM STATION);
PREPARE STMT1 FROM 'SELECT ROUND(LAT_N,4) FROM STATION ORDER BY LAT_N LIMIT ?,1';
EXECUTE STMT1 USING @foo;
END IF;
END$$
DELIMITER ;
CALL latmed()
我觉得它有点长,所以如果有人有更短的代码可以回答我的问题。谢谢
我是 MySQL 的新手,我正在尝试根据 case when 子句的条件执行不同的准备语句。问题是找到 table STATION 的 LAT_N 列的中位数,我想在 LAT_N 记录的偶数和奇数时使用 Case。
这是我为奇数情况准备的声明:
SET @foo = (SELECT ROUND((COUNT(*)-1)/2) FROM STATION);
PREPARE STMT1 FROM 'SELECT ROUND(LAT_N,4) FROM STATION ORDER BY LAT_N LIMIT ?,1';
EXECUTE STMT1 USING @foo;
这是偶数:
SET @foo1 = (SELECT ROUND(COUNT(*)/2) FROM STATION);
SET @foo2 = (SELECT ROUND((COUNT(*)+2)/2) FROM STATION);
PREPARE STMT2 FROM 'SELECT ROUND((((SELECT ROUND(LAT_N,4) FROM STATION ORDER BY LAT_N LIMIT ?,1) + (SELECT ROUND(LAT_N,4) FROM STATION ORDER BY LAT_N LIMIT ?,1))/2),4)';
EXECUTE STMT2 USING @foo1, @foo2;
而我的想法和这段代码有点相似:
SELECT CASE WHEN COUNT(*)%2=0 THEN EXECUTE STMT2 USING @foo1, @foo2
ELSE EXECUTE STMT1 USING @foo
FROM STATION
END;
但是那个代码 returns 错误 1064 (42000) 所以我想知道是否有任何替代方法。 非常感谢
我找到了这个问题的答案,其中包括程序的使用
DELIMITER $$
CREATE PROCEDURE latmed()
BEGIN
IF (SELECT COUNT(*) FROM STATION)%2=0 THEN
SET @foo1 = (SELECT ROUND(COUNT(*)/2) FROM STATION);
SET @foo2 = (SELECT ROUND((COUNT(*)+2)/2) FROM STATION);
PREPARE STMT2 FROM 'SELECT ROUND((((SELECT ROUND(LAT_N,4) FROM STATION ORDER BY LAT_N LIMIT ?,1) + (SELECT ROUND(LAT_N,4) FROM STATION ORDER BY LAT_N LIMIT ?,1))/2),4)';
EXECUTE STMT2 USING @foo1, @foo2;
ELSE
SET @foo = (SELECT ROUND((COUNT(*)-1)/2) FROM STATION);
PREPARE STMT1 FROM 'SELECT ROUND(LAT_N,4) FROM STATION ORDER BY LAT_N LIMIT ?,1';
EXECUTE STMT1 USING @foo;
END IF;
END$$
DELIMITER ;
CALL latmed()
我觉得它有点长,所以如果有人有更短的代码可以回答我的问题。谢谢