码迷,mamicode.com
首页 > 其他好文 > 详细

判断记录是否存在,存在更新,不存在插入的方案

时间:2017-07-15 11:17:03      阅读:631      评论:0      收藏:0      [点我收藏+]

标签:now()   sel   变量   play   style   查询   log   sed   else   

 

 

技术分享
BEGIN
    #定义一个变量来保存该记录是否存在
    declare num int;
    #这条sql,就是查询对应的记录有多少条,注意 into num 这两句话,就是把count(*) 查出的值,赋给到num中
    select count(*) into num from t_count_view where TO_DAYS(now())=TO_DAYS(day);
    #接下来的就是判断了,注意,判断是否等于,只有一个等于号
    if(num=0)
    #等于号之后,还要写一个Then,代表条件成立后要执行的sql
        Then
        insert into t_count_view(view_people,view_num,day)values(1,1,now());
  #else可以直接用,不需要加then
    else
        update t_count_view set view_people=view_people+1;
    #但是当if使用完之后,一定要写end if,代表着if的条件判断结束了
  end if;
END
View Code

 

技术分享
<insert id="saveOrUpdate" >
  <selectKey keyProperty="count" resultType="int" order="BEFORE">
    select count(*) from country where id = #{id}
  </selectKey>
  <if test="count > 0">
    update country 
    set countryname = #{countryname},countrycode = #{countrycode} 
    where id = #{id}
  </if>
  <if test="count==0">
    insert into country values(#{id},#{countryname},#{countrycode})
  </if>
</insert>
View Code

 

判断记录是否存在,存在更新,不存在插入的方案

标签:now()   sel   变量   play   style   查询   log   sed   else   

原文地址:http://www.cnblogs.com/shixm/p/7181825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!