DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`top_seller`() BEGIN drop table if exists top_sellers_min; create temporary table top_sellers_min ENGINE = MEMORY select a.Id_Article , tip.reino_id as Reino, count(m.Id_Article) as contados from Movimientos m inner join Tickets t on m.Id_Ticket = t.Id_Ticket inner join Articles a on a.Id_Article = m.Id_Article inner join Tipos tip on a.tipo_id = tip.tipo_id inner join reinos r on r.id = tip.reino_id where t.Fecha between timestampadd(day,-7,util.VN_CURDATE()) and util.VN_CURDATE() group by m.Id_Article; drop table if exists top_seller; create temporary table top_seller ENGINE = MEMORY select a.Article, tmp.Id_Article , a.Foto as foto, a.tipo_id as Id_Tipo, tmp.Reino, r.reino as ReinoNombre, tip.Tipo as Tipo, tmp.contados from top_sellers_min tmp inner join Articles a on a.Id_Article = tmp.Id_Article inner join Tipos tip on a.tipo_id = tip.tipo_id inner join reinos r on r.id = tmp.Reino where tmp.Reino = 1 order by contados desc limit 5; insert into top_seller select a.Article, tmp.Id_Article , a.Foto as foto, a.tipo_id as Id_Tipo, tmp.Reino, r.reino as ReinoNombre, tip.Tipo as Tipo, tmp.contados from top_sellers_min tmp inner join Articles a on a.Id_Article = tmp.Id_Article inner join Tipos tip on a.tipo_id = tip.tipo_id inner join reinos r on r.id = tmp.Reino where tmp.Reino = 2 order by contados desc limit 5; insert into top_seller select a.Article, tmp.Id_Article , a.Foto as foto, a.tipo_id as Id_Tipo, tmp.Reino, r.reino as ReinoNombre, tip.Tipo as Tipo, tmp.contados from top_sellers_min tmp inner join Articles a on a.Id_Article = tmp.Id_Article inner join Tipos tip on a.tipo_id = tip.tipo_id inner join reinos r on r.id = tmp.Reino where tmp.Reino = 3 order by contados desc limit 5; insert into top_seller select a.Article, tmp.Id_Article , a.Foto as foto, a.tipo_id as Id_Tipo, tmp.Reino, r.reino as ReinoNombre, tip.Tipo as Tipo, tmp.contados from top_sellers_min tmp inner join Articles a on a.Id_Article = tmp.Id_Article inner join Tipos tip on a.tipo_id = tip.tipo_id inner join reinos r on r.id = tmp.Reino where tmp.Reino = 4 order by contados desc limit 5; insert into top_seller select a.Article, tmp.Id_Article , a.Foto as foto, a.tipo_id as Id_Tipo, tmp.Reino, r.reino as ReinoNombre, tip.Tipo as Tipo, tmp.contados from top_sellers_min tmp inner join Articles a on a.Id_Article = tmp.Id_Article inner join Tipos tip on a.tipo_id = tip.tipo_id inner join reinos r on r.id = tmp.Reino where tmp.Reino = 5 order by contados desc limit 5; insert into top_seller select a.Article, tmp.Id_Article , a.Foto as foto, a.tipo_id as Id_Tipo, tmp.Reino, r.reino as ReinoNombre, tip.Tipo as Tipo, tmp.contados from top_sellers_min tmp inner join Articles a on a.Id_Article = tmp.Id_Article inner join Tipos tip on a.tipo_id = tip.tipo_id inner join reinos r on r.id = tmp.Reino where tmp.Reino = 7 order by contados desc limit 5; drop table if exists top_sellers_min; END$$ DELIMITER ;