Does TiDB have an implementation similar to MERGE INTO?

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: tidb有没有类似MERGE INTO 的实现

| username: zhanggame1

In our business, we have an operation where we insert several records into a table. If there are no records, we insert new ones; if there are existing records, we update them. This is similar to the merge support in other databases. When updating, the requirement is to add the inserted values to the original numbers and save the result.

| username: 我是咖啡哥 | Original post link

Use replace

| username: zhanggame1 | Original post link

Replace deletes the original data. Here, we hope to modify the original data, for example, if the original data is 10, modify it to 10 plus 5.

| username: 春风十里 | Original post link

The “merge into” is Oracle’s syntax, which updates or inserts into another table based on a field match from one table, effectively combining update and insert into one statement, making the syntax relatively concise. In MySQL, there is no “merge into” syntax. The usual practice is to use two statements, insert + update, as a replacement, or use INSERT INTO … ON DUPLICATE KEY UPDATE, or replace for partial replacement. I also didn’t find “merge into” in the TiDB documentation, so it seems it doesn’t exist.

| username: zhanggame1 | Original post link

I checked the information, and they all write update and insert separately, using exists to determine.

| username: redgame | Original post link

It is estimated that it will be added later, for now, it needs to be done on the application.

| username: linnana | Original post link

Looking forward to new features.

| username: WinterLiu | Original post link

Learned.

| username: ShawnYan | Original post link

Strictly speaking, this statement is not accurate. SQL syntax is written in the standard, and Oracle just introduced it a bit earlier. Other relational databases also have their own implementations.

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.