Ti posto un esempio che grosso modo dovrebbe ricalcare il tuo problema sperando ti sia utile.
codice:
create table lingue (
id int not null auto_increment primary key,
lingua varchar(10)
) engine = innodb;
create table articoli (
id int not null auto_increment primary key,
id_articolo int,
id_lingua int,
testo varchar(50),
foreign key (id_lingua) references lingue(id) on delete cascade
) engine = innodb;
insert into lingue (lingua) values ('ita'),('fra'),('eng');
insert into articoli (id_articolo,id_lingua,testo)
values
(1,1,'testo1_ita'),
(1,3,'testo1_eng'),
(2,2,'testo2_fra'),
(3,2,'testo3_fra'),
(3,3,'testo3_eng');
La query "statica" sarebbe questa:
codice:
select a.id_articolo,
max(case when id_lingua = 1 then testo else '' end) as testo_ita,
max(case when id_lingua = 2 then testo else '' end) as testo_fra,
max(case when id_lingua = 3 then testo else '' end) as testo_eng
from lingue as l
left join articoli as a
on l.id = a.id_articolo
group by a.id_articolo;
che produrrebbe questo risultato:
codice:
+-------------+------------+------------+------------+
| id_articolo | testo_ita | testo_fra | testo_eng |
+-------------+------------+------------+------------+
| 1 | testo1_ita | | testo1_eng |
| 2 | | testo2_fra | |
| 3 | | testo3_fra | testo3_eng |
+-------------+------------+------------+------------+
La stored procedure per rendere la query dinamica è così:
codice:
delimiter //
drop procedure if exists query_dinamica//
create procedure query_dinamica()
begin
declare finito int default 0;
declare cid int;
declare clingua varchar(10);
declare str varchar(10000) default "select a.id_articolo,";
declare cursore cursor for select id,lingua from lingue;
declare continue handler for not found set finito = 1;
open cursore;
mio_loop:loop
fetch cursore into cid,clingua;
if finito = 1 then
leave mio_loop;
end if;
set str = concat(str, "max(case when id_lingua = ",cid," then testo else '' end) as testo_",clingua,",");
end loop;
close cursore;
set str = substr(str,1,char_length(str)-1);
set @str = concat(str," from lingue as l
left join articoli as a
on l.id = a.id_articolo
group by a.id_articolo");
prepare stmt from @str;
execute stmt;
deallocate prepare stmt;
-- select str;
end;//
delimiter ;
che richiamata produce lo stesso output della query statica.
Proviamo ad aggiungere una nuova lingua e un paio di record:
codice:
insert into lingue (lingua) values ('ted');
insert into articoli (id_articolo,id_lingua,testo) values (4,1,'testo4_ita'),(4,4,'testo4_ted');
e a richiamare la sp:
codice:
mysql> call query_dinamica;
+-------------+------------+------------+------------+------------+
| id_articolo | testo_ita | testo_fra | testo_eng | testo_ted |
+-------------+------------+------------+------------+------------+
| 1 | testo1_ita | | testo1_eng | |
| 2 | | testo2_fra | | |
| 3 | | testo3_fra | testo3_eng | |
| 4 | testo4_ita | | | testo4_ted |
+-------------+------------+------------+------------+------------+
Infine, proviamo a cancellare una lingua e, di conseguenza, i record annessi.
codice:
mysql> delete from lingue where lingua = 'fra';
Query OK, 1 row affected (0.00 sec)
mysql> call query_dinamica;
+-------------+------------+------------+------------+
| id_articolo | testo_ita | testo_eng | testo_ted |
+-------------+------------+------------+------------+
| 1 | testo1_ita | testo1_eng | |
| 3 | | testo3_eng | |
| 4 | testo4_ita | | testo4_ted |
+-------------+------------+------------+------------+
Ciao.