mysql insert or replace 使用
时间: 2024-02-16 12:03:40 浏览: 74
MySQL does not have an `INSERT OR REPLACE` statement like SQLite. However, you can achieve similar functionality using the `INSERT INTO ... ON DUPLICATE KEY UPDATE` statement.
Assuming you have a table named `my_table` with columns `id`, `name`, and `age`, and `id` is the primary key column, you can use the following syntax to insert a new row or update an existing row with the same `id` value:
```
INSERT INTO my_table (id, name, age) VALUES (1, 'John', 30)
ON DUPLICATE KEY UPDATE name = VALUES(name), age = VALUES(age);
```
In this example, if a row with `id` = 1 already exists, it will update the `name` and `age` columns with the new values. If no row with `id` = 1 exists, it will insert a new row with the specified values.
Note that `VALUES(name)` and `VALUES(age)` refer to the values specified in the `INSERT` statement. This allows you to update the row with the new values if it already exists.
阅读全文