Mysql存储过程的一个实例

    DELIMITER //
     CREATE PROCEDURE GetAllProducts()
       BEGIN
          declare count int;
          select count(*) into count from shopcar where openid=open_id and item_no=itemno;
          if(count=0)
            then
            insert into shopcar (openid,item_no,item_num) values (open_id,itemno,itemnum);
          else
            update shopcar set item_num=item_num+itemnum where openid=open_id and item_no=itemno;
          end if;
       END //

    DELIMITER ;

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

猜你喜欢

转载自blog.csdn.net/gdali/article/details/80295128
今日推荐