在ON DUPLICATE KEY UPDATE列中使用WHERE子句

时间:2016-12-02 04:18:11

标签: mysql sql

这是cartitems表的表格设计

cartitems

cartitem_id PK AI
customer_id FK
item_id FK UNIQUE 
item_quantity INT DEFAULT 1

我需要完成的事情

1。)如果表格中存在item_id,则每当用户点击“添加到购物车”按钮时,将item_quantity增加 1 对于相同 item_id

2。)如果表中尚不存在item_id,请运行插入语句。

所以我做的就是这个。

CREATE DEFINER=`root`@`localhost` PROCEDURE `addItemToCart`(aCustomerId int, aProductId int)
    BEGIN
        INSERT INTO cart_items(customer_id,product_id) 
        VALUES(aCustomerId,aProductId)

        ON DUPLICATE KEY UPDATE
        item_quantity = item_quantity + 1

        WHERE customer_id = aCustomerId AND item_id = aProductId;
    END

但是当我查看它时,我收到一条错误,missing semicolon item_quantity = item_quantity + 1

我无法弄清楚导致错误的原因。我不知道WHERE子句是否存在问题。

我很感激任何帮助。

感谢。

3 个答案:

答案 0 :(得分:0)

ON DUPLICATE KEY UPDATE

中详细解释了UPDATE的行为
  

如果指定ON DUPLICATE KEY UPDATE,并且插入的行会导致UNIQUE索引或PRIMARY KEY中出现重复值,则MySQL会执行旧行的UPDATE

查看您的表格,您希望增加给定客户购物车的商品数量。我在这里假设客户一次只能有一个购物车。因此,您希望MySQL执行INSERT而不是CREATE UNIQUE INDEX unq_item_index ON cart_items (customer_id, item_id); 的条件是客户已经出现在表中。

为此,您可以通过以下方式在这两列上创建唯一索引:

INSERT

然后,您可以使用以下INSERT INTO cart_items (customer_id, item_id) VALUES (aCustomerId, anItemId) ON DUPLICATE KEY UPDATE item_quantity = item_quantity + 1 WHERE customer_id = aCustomerId AND item_id = anItemId; 查询:

item_quantity

现在的行为是,如果客户/项目的新条目进入,1将设置为默认值item_quantity,否则1将会增加struct timespec request; request.tv_sec = 0; request.tv_nsec = 17000000; // 17ms mach_timebase_info_data_t base; mach_timebase_info(&base); while (1) { uint64_t start, end, elapsed; start = mach_absolute_time(); nanosleep(&request, NULL); end = mach_absolute_time(); elapsed = (end - start) / base.denom; printf("Sleep = %f msec\n", elapsed * 1E-6); }

答案 1 :(得分:0)

BEGIN

IF NOT EXISTS(SELECT 1 FROM cart_items WHERE item_id = aProductId)
BEGIN
  INSERT INTO cart_items(customer_id,product_id) 
  VALUES(aCustomerId,aProductId)
END
ELSE  
  UPDATE cart_items SET item_quantity = item_quantity + 1 WHERE customer_id = aCustomerId AND item_id = aProductId;
END

答案 2 :(得分:0)

为什么要在那里使用DUPLICATE KEY条款?这没有意义。如果发生WHERE,它将更新您指定给具有相同密钥的旧记录的任何字段。所以,如果你只是删除INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1; UPDATE table SET c=c+1 WHERE a=1; 子句,它应该工作。检查this article。它陈述了以下两个:

public class RecylerAdapter extends  RecyclerView.Adapter<RecylerAdapter.ViewHolder>
{   private boolean isSelected;
    private final static int FADE_DURATION = 500;// milliseconds
    private int lastPosition = -1;
    Context cont;
  private  String[] strname;
    private int[] icon;
    public RecylerAdapter(Context con, String[] androidNames, int[] androidIcon)
    {   cont=con;
        strname=androidNames;
        icon=androidIcon;
    }
    class ViewHolder extends RecyclerView.ViewHolder
    {   private ImageView imgView;
        private TextView txtView;
        private CardView cardView;
        private SparseBooleanArray selectedItems = new SparseBooleanArray();

        public ViewHolder(final View itemView)
        {
            super(itemView);

            imgView = (ImageView) itemView.findViewById(R.id.imageView);
            txtView = (TextView) itemView.findViewById(R.id.txt);
            cardView = (CardView) itemView.findViewById(R.id.cv12);


            itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    cardView.isSelected = !cardView.isSelected;
                    notifyDataSetChanged();
                }
            });
        }
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout,parent,false);
        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(ViewHolder holder, int i)
    {
        if(ViewHolder.isSelected)
        {
            holder.cardView.setBackground(Color.Green);
        }
        else{
            holder.cardView.setBackground(Color.Pink);
        }
        holder.txtView.setText(strname[i]);
        holder.imgView.setImageResource(icon[i]);
        setAnimation(holder.cardView, i);
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setAnimation(View viewToAnimate, int position)
    {
        // If the bound view wasn't previously displayed on screen, it's animated
        if (position > lastPosition)
        {
            //animation 1
            AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
            anim.setDuration(FADE_DURATION);
            viewToAnimate.startAnimation(anim);

            //animation 2
            Animation animation = AnimationUtils.loadAnimation(cont, android.R.anim.slide_in_left);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
        else
        {
            Animation animation = AnimationUtils.loadAnimation(cont, android.R.anim.slide_in_left);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
    }
    @Override
    public int getItemCount()
    {
        return strname.length;
    }
    public void setSelected(boolean selection){
        this.isSelected = selection;
    }
    public boolean isSelected(){
        return isSelected;
    }
}

是一样的。