将预准备语句的结果存储为mysql中的表?
作者:互联网
是否可以在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另外提及,为什么这不适用于准备好的语句.
解决方法:
将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
标签:mysql,prepared-statement,random-sample,create-table 来源: https://codeday.me/bug/20190629/1323414.html