Saving the results of a prepared statement as a table in mysql?

Is it possible to save the result of a prepared table in mysql?

My use case is:: I create two variables based on certain conditions of the original table, and then get randomized rows based on these criteria. Since I have 10 such tables, I must be the first to join them, and then perform this randomization according to the β€œgeneral” criteria for passing / filtering (see also @total below, which is my main criterion, the PER table)

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;

After executing this statement - I want to store these lines for later search, preferably in the form of a table. I would like to do something like this

# incorrect syntax, but I would like something similar 
create table tab_derived_1
select * from 
EXECUTE STMT USING @sample;

Tip: +1 for an additional mention of why this does not work with prepared statements.

+4
1

create :

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
+3

Source: https://habr.com/ru/post/1523345/


All Articles