0

我希望在只有 2 列的表中同时插入许多值,如果这些值已经存在,则必须对其进行更新。虽然 1 列可以重复,但第二列不能重复。我可以很容易地做到这一点以下查询..但问题是这里只能考虑一行...没有主键.. PLZ HELP

INSERT INTO `table` (value1, value2) 
SELECT 'stuff for value1', 'stuff for value2' FROM `table` 
WHERE NOT EXISTS (SELECT * FROM `table` 
                  WHERE value1='stuff for value1' AND value2='stuff for value2') 
LIMIT 1
4

1 回答 1

2

尝试在重复键更新集column1 = ......上插入表名......

替代方式::

Step1:创建一个与table1结构相同的temp_table

第2步:

    INsert into temp_Table 
   (SELECT * from table1 t1 left join table2 t2 on (t1.value1=t2.value1 and t1.value2=t2.value2)
    where t2.value1 is null and t2.value2 is null);

第三步:

  INsert into table Select * from temp_table 
于 2012-06-21T09:27:24.433 回答