CREATE DEFINER=`root`@`%` PROCEDURE `get_news_predictions_by_id`(user_id integer unsigned, count integer unsigned)
BEGIN
declare flag integer default 0;
declare i integer;
declare j integer;
declare cur1 cursor for select news_type_id,predictnum from proportion order by news_type_id;
declare continue handler for not found set flag=1;
set @query=concat('create temporary table viewed (select c.news_id, n.news_type_id from news_click c left join news n on c.news_id=n.news_id where c.clicker_id=',user_id,' order by clicking_time limit 0,',count,')');
prepare stmt from @query;
execute stmt;
create temporary table proportion (select count(*) as countnum, count(*) as predictnum, news_type_id from viewed group by news_type_id);
set @temp=(select @sum:=sum(countnum) from proportion);
create temporary table prediction0 (select news_id,news_type_id from news where 1=2);
if @sum>0 then
if @sum<count then
update proportion set predictnum=countnum/@sum*count;
end if;
open cur1;
buildo:repeat
fetch cur1 into i,j;
if flag then
leave buildo;
end if;
set @query=concat('insert into prediction0 select news_id,news_type_id from news where news_type_id=',i,' and news_id not in (select news_id from viewed) order by initial_time, local_publish_time limit 0,',j);
prepare stmt from @query;
execute stmt;
until flag end repeat;
close cur1;
select n.*,t.name type_name from prediction0 pre left join news n on pre.news_id=n.news_id left join news_type t on pre.news_type_id=t.news_type_id;
else
set @query=concat('select n.*,t.name type_name from news n left join news_type t on n.news_type_id=t.news_type_id order by initial_time,local_publish_time desc limit 0,',count);
prepare stmt from @query;
execute stmt;
end if;
END
|