Does replace into have a where clause?

后端 未结 2 939
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 09:48

I\'m writing an application and I\'m using MySQL as DBMS, we are downloading property offers and there were some performance issues. The old architecture looked like this: A

2条回答
  •  时光说笑
    2020-11-30 10:26

    I can see that you have solved your problem, but to answer your original question:

    REPLACE INTO does not have a WHERE clause.

    The REPLACE INTO syntax works exactly like INSERT INTO except that any old rows with the same primary or unique key is automaticly deleted before the new row is inserted.

    This means that instead of a WHERE clause, you should add the primary key to the values beeing replaced to limit your update.

    REPLACE INTO myTable (
      myPrimaryKey,
      myColumn1,
      myColumn2
    ) VALUES (
      100,
      'value1',
      'value2'
    );
    

    ...will provide the same result as...

    UPDATE myTable
    SET myColumn1 = 'value1', myColumn2 = 'value2'
    WHERE myPrimaryKey = 100;
    

    ...or more exactly:

    DELETE FROM myTable WHERE myPrimaryKey = 100;
    INSERT INTO myTable(
      myPrimaryKey,
      myColumn1,
      myColumn2
    ) VALUES (
      100,
      'value1',
      'value2'
    );
    

提交回复
热议问题