Does replace into have a where clause?

后端 未结 2 920
伪装坚强ぢ
伪装坚强ぢ 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'
    );
    
    0 讨论(0)
  • 2020-11-30 10:31

    In your documentation link, they show three alternate forms of the replace command. Even though elided, the only one that can accept a where clause is the third form with the trailing select.

    replace seems like overkill relative to update if I am understanding your task properly.

    0 讨论(0)
提交回复
热议问题