现在没有数据,表字段:id,shop_id,title,content,addtime,recommend_sign

解决方案 »

  1.   

    现在没有数据,表字段:id,shop_id,title,content,addtime,recommend_sign那就辛苦一下自己编一些能说明问题的测试数据。
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://bbs.csdn.net/topics/320211382
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
      

  2.   

    create procedure test()
    begin
              declare v_shop_id int;
              declare v_flag int default 0;
              declare p cursor for select shop_id from info;
              declare continue handler for not found set v_flag=2;
     
               drop table if exists info_tmp;  // info 就是你的资讯表
               create table info_tmp like info;
               open p;
              l1:loop
                          fetch p into v_shop_id;
                          if v_flag=1 then leave l1 end if;
                          insert into info_tmp select * from info where shop_id=v_shop_id order by addtime  limit 2;
              end loop l1;
    end;
    利用游标来实现,语法你在调一下,可以实
      

  3.   

    select * from info where shop_id=v_shop_id order by addtime  desc  limit 2;时间按降序排一下,达到最新时间的要求。
      

  4.   


    在addtime上建个索引,然后用这个方法应该比较高效