mysql insert or replace 使用
时间: 2024-02-16 10:03:40 浏览: 33
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.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)