MySQL Insert Where query

前端 未结 27 2396
悲&欢浪女
悲&欢浪女 2020-11-22 06:16

What\'s wrong with this query:

INSERT INTO Users( weight, desiredWeight ) VALUES ( 160, 145 ) WHERE id = 1;

It works without the WHE

27条回答
  •  佛祖请我去吃肉
    2020-11-22 06:49

    DO READ THIS AS WELL

    It doesn't make sense... even literally

    INSERT means add a new row and when you say WHERE you define which row are you talking about in the SQL.

    So adding a new row is not possible with a condition on an existing row.

    You have to choose from the following:

    A. Use UPDATE instead of INSERT

    B. Use INSERT and remove WHERE clause ( I am just saying it...) or if you are real bound to use INSERT and WHERE in a single statement it can be done only via INSERT..SELECT clause...

    INSERT INTO Users( weight, desiredWeight ) 
    SELECT FROM Users WHERE id = 1;
    

    But this serves an entirely different purpose and if you have defined id as Primary Key this insert will be failure, otherwise a new row will be inserted with id = 1.

提交回复
热议问题