语句如下 select count(id) t_count from cet6 where word='a';   
if t_count> 0 then    
update set cet68=2,total=total+2 where word='a';   
else  
insert into cet6(word,total,cet68) values('a',2,2);   
end;  

解决方案 »

  1.   

    select count(id) t_count from cet6 where word='a';      
    if t_count> 0 then       
    update cet6 set cet68=2,total=total+2 where word='a';      
    else     
    insert into cet6(word,total,cet68) values('a',2,2);      
    end;    
      

  2.   


    这些语句一定要在存储过程或者存储函数里面做才可以!declare t_count int unsigned default 0;
    select count(id) into t_count from cet6 where word='a';   
    if t_count> 0 then    
    update cet6 set cet68=2,total=total+2 where word='a';   
    else  
    insert into cet6(word,total,cet68) values('a',2,2);   
    end if;  仔细看手册!熟悉语法!
      

  3.   

    mysql中不支持直接运行这样的语句块,必须放在存储过程或函数里面来作为一个整体处理你上面的语句漏了into,且变量要定义,当然,也可以用用户变量,帮你改了下:
    select count(id) into @t_count from cet6 where word='a';   
    if @t_count > 0 then    
    update set cet68=2,total=total+2 where word='a';   
    else  
    insert into cet6(word,total,cet68) values('a',2,2);   
    end;  把上面的语句块代码建立个存储过程执行就可以了。
      

  4.   

    存储过程的相关知识点请参考官方文档:http://dev.mysql.com/doc/refman/5.1/zh/stored-procedures.html
      

  5.   

    MySQL 与其它数据库不一样,不向ORALCE , SQL SERVER中支持匿名块。这个IF THEN语句只能用到存储过程或者触发器中。
      

  6.   

    1、你的语句要放在SP、UDF中执行;
    2、
    declare t_count int unsigned default 0;
    select count(id) into t_count from cet6 where word='a';   
    if t_count> 0 then    
    update cet6 set cet68=2,total=total+2 where word='a';   
    else  
    insert into cet6(word,total,cet68) values('a',2,2);   
    end if;  
      

  7.   

    其实你的功能一在MYSQL中一句SQL就行了。
    设置你的 word 为主键或者唯一键。insert into cet6(word,total,cet68) values('a',2,2)
    on duplicate key update cet68=2,total=total+2;