大意是,从SUBJECT表里获取对应的大类PID的对应的数据的SID,然后将这些SID随机的给ARTICLE表里的SID字段赋值;小弟MYSQL不熟悉,请帮忙下
DECLARE  @len INT;
DECLARE  @i  INT;
DECLARE  @v INT; 
set @len=(select count(*) from jxmsw_subject where pid=1);
set @i=0;
while @i<=@len
begin
set @i=@i+1;
set @v=(select top 1 sid from jxmsw_subject where pid=1 order by newid());
update jxmsw_articles set sid = @v where sid<> 0 and articleid = (select top 1 articleid from jxmsw_articles where articleid not in (select top (@i-1) articleid from jxmsw_articles where sid<> 0) and catid=1 order by articleid asc);
end;

解决方案 »

  1.   

    create procdure sp_test()
    begin
    DECLARE  len INT;
    DECLARE  i  INT;
    DECLARE  v INT; 
    select count(*) into len from jxmsw_subject where pid=1;
    set i=0;
    while i<=@len do
    set i=i+1;
    select sid into v from jxmsw_subject where pid=1 order by newid() limit 1;
    update jxmsw_articles set sid = v where sid<> 0 and articleid in (select  articleid from jxmsw_articles where articleid not in (select articleid from jxmsw_articles where sid<> 0) and catid=1 order by articleid asc);
    end while;
    end
      

  2.   


    DECLARE  len INT; 这里报错,难道是PHPMYADMIN版本问题,我查看了资料,应该没什么问题才对啊
      

  3.   


    DECLARE len INT;
    DECLARE i INT;
    DECLARE v INT;
    SELECT count(*),0 INTO len,i FROM jxmsw_subject WHERE pid=1
    while i<len
    begin
     set i=i+1;
     select sid into v from jxmsw_subject where pid=1 order by rand() limit 1;
     set @sql=concat('update jxmsw_articles set sid=v where sid<>0 and articaleid=(select articleid from jxmsw_articles where articaleid not in(select articleid from jxmsw_articles where sid<>0 limit ',i,') and and catid=1 order by articleid asc limit 1))';
     prepare stmt from @sql;
     execute stmt;
     deallocate prepare stmt;
    end
    -- 楼主测试一下,我菜鸟
      

  4.   


    不好意思。 我while都还没学会
    把这里改一下
    while i<len do
    end while学习2楼
      

  5.   

    DECLARE len INT; 这里报错
      

  6.   

    到底什么问题啊,服务器上和本地,都报错,上面大哥,给的SQL语句是没错的,郁闷的
      

  7.   

    楼主需要先浏览一下MYSQL的手册中关于存储过程的语法,10分钟就可以浏览完毕。然后有问题,如果是语法上的就直接参考一下手册,或者把你自己的代码和错误信息一同贴出。MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html问题说明越详细,回答也会越准确!参见如何提问。(提问的智慧
        
      

  8.   

    知道了,是PHPMYADMIN的版本,不支持存储过程造成的