通过select插入表前如何检查重复项:; G. r% V, Z6 Z; g7 h/ y
insert into table1select col1,col2 from table2我需要检查table是否已有table1.col1.value = table2.col1.value如果是,从插入中排除行。 ! {" M& J& ?; \* X* l$ w% c' W$ h 6 }2 `3 L" K% z. d! o3 f1 s% }( D解决方案: * _/ ]% A4 f9 J% l0 v$ y INSERT INTO table1 SELECT t2.col t2.col2 FROM table2 t2 LEFT JOIN table1 t1 ON t2.col1 = t1.col1 AND t2.col2 = t1.col2 WHERE t1.col1 IS NULL替代使用 8 n6 j( `5 _- A& [* e% \: U8 ~9 rINSERT INTO @table2 SELECT col col2 FROM table1 EXCEPT SELECT t1.col t1.col2 FROM table1 t1 INNER JOIN table2 t2 ON t1.col1 = t2.col1 AND t1.col2 = t2.col2使用不存在的替代方法 0 C }0 k' c7 a0 k1 @" |INSERT INTO table2 SELECT col1,col2 FROM table1 t1WHERENOT EXISTS( SELECT 1 FROM table2 t2 WHERE t1.col1 = t2.col1 AND t1.col2 = t2.col2)