Chinaunix首页 | 论坛 | 博客
  • 博客访问: 57026
  • 博文数量: 5
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-06 22:22
文章分类

全部博文(5)

文章存档

2009年(17)

我的朋友
最近访客

分类: Mysql/postgreSQL

2009-12-03 03:17:24

别看就40行,也是我写过得最长的,为了一个很简单的功能查遍天涯海角……
您可以批评说算法烂,没办法,一是我没怎么学过算法,二是实在赶时间,离完工没几天了,争分夺秒。


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


阅读(558) | 评论(0) | 转发(0) |
0

上一篇:子句与组合成分

下一篇:抱歉

给主人留下些什么吧!~~