我有两个结构相同的表
**t1**
+------+------+
| code | vid |
+------+------+
| AAA | 2 | - row1
| BBB | 3 | - row2
| AAA | 4 | - row3
| CCC | 2 | - row4
+------+------+
**t2**
+------+------+
| code | vid |
+------+------+
| AAA | 2 | - row1
| BBB | 3 | - row2
| AAA | 4 | - row3
| CCC | 3 | - row4
| DDD | 4 | - row5
+------+------+
**Result table**
**t1**
+------+------+
| code | vid |
+------+------+
| AAA | 2 | - row1
| BBB | 3 | - row2
| AAA | 4 | - row3
| CCC | 2 | - row4
| CCC | 3 | - row5 New Inserted Row
| DDD | 4 | - row6 New Inserted Row
+------+------+
我想从表2到表1插入新记录。行row4和row5是未匹配的行,我想在表1中添加。 我怎么能这样做?
提前致谢!
答案 0 :(得分:1)
尝试此查询 -
INSERT INTO t1
SELECT t2.* FROM t2
LEFT JOIN t1
ON t1.code = t2.code AND t1.vid = t2.vid
WHERE t1.code IS NULL AND t1.vid IS NULL