在查询中使用预准备语句的结果作为临时表

时间:2016-12-14 21:03:28

标签: mysql prepared-statement

如果我有预备陈述的输出;如何将其用作查询的来源?

CALL `myProcedure`;

PREPARE stmnt FROM @allSQL;

EXECUTE stmnt;

DEALLOCATE PREPARE stmnt;

所以,我从EXECUTE stmnt;得到一个输出(让我们称之为tmp),我想按照以下方式运行查询:

SELECT * FROM (EXECUTE stmnt) AS tmp WHERE this = that;

1 个答案:

答案 0 :(得分:2)

您可以准备一个包含查询结果的CREATE TEMPORARY TABLE语句:

SET @createSQL = CONCAT('CREATE TEMPORARY TABLE tmp AS ', @allSQL);
PREPARE stmt FROM @createSQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SELECT * FROM tmp WHERE this = that;