Crea una nuova tabella che chiameremo risultati che conterrà l'elenco delle tabelle e il valore che memorizzerai (si/no).
codice:
create table risultati (
id int not null auto_increment primary key,
tabella varchar(50),
valore char(2)
);
insert into risultati (tabella)
select table_name from information_schema.tables
where table_schema = 'nome_database' and table_name <> 'risultati'
La stored procedure sarà così
codice:
delimiter //
create procedure cicla(in k int)
begin
declare finite int default 0;
declare tab varchar(50);
declare str varchar(200);
declare cur_tabella cursor for select table_name from information_schema.tables where table_schema = 'nome_database' and table_name <> 'risultati';
declare continue handler for not found set finite = 1;
open cur_tabella;
mio_loop:loop
fetch cur_tabella into tab;
if finite = 1 then
leave mio_loop;
end if;
set @str = concat("update risultati
set valore =
case when (select sum(if(matching='si',1,0)) from (select matching from ",tab," order by id limit ",k,") as t) > 0
then 'si' else 'no' end where tabella = '",tab,"'");
prepare stmt from @str;
execute stmt;
end loop;
close cur_tabella;
end //
delimiter ;
Tramite la quale ciclerai tutte le tabelle ad esclusione di quella risultati che verrà popolata all'interno del ciclo.
La richiamerai semplicemente così:
call cicla(4);
Ovviamente fatti un dump preventivo e poi testala.