zoukankan      html  css  js  c++  java
  • 判断记录是否存在,存在更新,不存在插入的方案

    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
  • 相关阅读:
    CSS3实现翻转菜单效果
    C语言根据日期取其位于一年中的第几天
    实习第一周小记------生活不易
    [置顶] iOS开发规范
    理解 Neutorn LBaaS
    FWaaS 实践: 允许 ssh
    实践 Neutron FWaaS
    理解 Neutron FWaaS
    应用新安全组
    Neutron 默认安全组规则
  • 原文地址:https://www.cnblogs.com/shixm/p/7181825.html
Copyright © 2011-2022 走看看