CREATE TABLE `userInfo` (
  `userName` char(64) NOT NULL COMMENT '用户名\0e\0',
  `` int(11) NOT NULL,
  PRIMARY KEY  (`userName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `userDetail` (
  `userName` char(64) NOT NULL COMMENT '用户名\0e\0',
  `DoTime` int(11) NOT NULL, 
  PRIMARY KEY  (`userName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;我有两个表userInfo,同userDetail,现在我想实现  对DoTime>100的所有用户积分 都减去DoTime*100; 请问存储过程怎么写

解决方案 »

  1.   

    如果你的描述是准确的话,则不需要存储过程,一句SQL语句就搞定。update userDetail 
    set DoTime=DoTime-DoTime*100
    where DoTime>100;
      

  2.   

    update a from `userInfo` a
    inner join
    `userDetail` b on a.`userName`=b.`userName` 
    set a.``=b.DoTime*100where b.DoTime>100
      

  3.   

    楼主说的这个需求不是太明白,按照字面上的理解的话
    update userDetail set Dotime=Dotime-DoTime*100 where Dotime>100;
      

  4.   

    积分:update userDetail d inner join userInfo i using (userName)
    set =-DoTime*100
    where DoTime>100;
      

  5.   

    update a from `userInfo` a 
    inner join 
    `userDetail` b on a.`userName`=b.`userName` 
    set a.``=a.``-b.DoTime*100 where b.DoTime>100
      

  6.   

    另外两种变形的写法, 其实都一样update userDetail d inner join userInfo i on d.userName=i.userName
    set =-DoTime*100
    where DoTime>100;
    update userDetail d ,userInfo i 
    set =-DoTime*100
    where DoTime>100
    and d.userName=i.userName
      

  7.   

    我要对表userInfo中的userName的进行更新, 更新后的值为 原来的值减去userName的doTime*40当userName的doTime>=100的时候我需要更新,否则不更新
      

  8.   


    update userDetail d inner join userInfo i using (userName)
    set =-DoTime*40
    where DoTime>100;update userDetail d inner join userInfo i on d.userName=i.userName
    set =-DoTime*40
    where DoTime>100;update userDetail d ,userInfo i 
    set =-DoTime*40
    where DoTime>100
    and d.userName=i.userName
      

  9.   

    update userDetail set dotime =dotime-dotime*40 where DoTime>=100
    and username = '' ;
    是这个意思吗?
      

  10.   

    delimiter $$
    create procedure test(out resultvalue int)
    begin
    declare v_userName varchar(32);
    declare v_doTime int(11);
    declare done int default 0;
    declare cur1 cursor for select userName,doTime from userDetail;
    declare exit handler for not found set done=1;
    set resultvalue=0;
    open cur1;
    repeat
    fetch cur1 into v_userName,v_doTime;
    if v_doTime>=100 then
    update userInfo set =-v_doTime*40 where userName=v_userName;
    else
    set resultvalue=1;
    end if;
    until done=1 end repeat;
    close cur1;
    end $$
    delimiter ;
      

  11.   

    update a from `userInfo` a 
    inner join 
    `userDetail` b on a.`userName`=b.`userName` 
    set a.``=a.``-b.DoTime*40 where b.DoTime>=100
      

  12.   

    or
    update `userInfo` a ,`userDetail` b 
    set a.``=a.``-b.DoTime*40 
    where b.DoTime>=100 and a.`userName`=b.`userName` 
      

  13.   

    这年头都喜欢没事找事用存储过程,可以用WHERE,having判断的,一律都喜欢放到fetch里面写if