是否可以将准备好的表的结果存储在mysql中?
我的用例是 - : 我根据源表的某些条件创建两个变量,然后根据此条件获取随机化的行。由于我有10个这样的表,我应该是第一个加入它们然后对“整体”传递/过滤标准进行随机化(参见下面的@total,这是我的主要标准,PER表)
set @total=(select count(*) from tab_1 where predict_var ="4" or predict_var ="2" ) ;
set @sample= ( select @total*(70/30)) ;
PREPARE STMT FROM " SELECT * FROM tab_1 WHERE predict_var = '4' or predict_var = '2' union
(SELECT * FROM tab_1 WHERE predict_var = '0' or predict_var = '1' ORDER BY RAND() limit ? )" ;
EXECUTE STMT USING @sample;
在我执行了这个语句之后 - 我希望存储这些行,以便以后检索,最好是以表格的形式。我想做这样的事情
# incorrect syntax, but I would like something similar
create table tab_derived_1
select * from
EXECUTE STMT USING @sample;
提示:+1另外提及,为什么这不适用于准备好的语句。
答案 0 :(得分:3)
将create table放在语句中:
PREPARE STMT FROM "CREATE TABLE tab_derived_1 SELECT * FROM tab_1 WHERE predict_var = '4' or predict_var = '2' union
(SELECT * FROM tab_1 WHERE predict_var = '0' or predict_var = '1' ORDER BY RAND() limit ? )" ;
EXECUTE STMT USING @sample;
如果你想返回结果,不只是将它们存储在一个表中,只需要做最后的
SELECT * FROM tab_derived_1