使用php将mysql表中的数据插入到另一个表中,跳过已经存在的数据
大家好,我解决不了我的这个问题:
我有两个表:
table_1 (id, cod。产品,颜色,价格,id_supplier)
table_2 (id_supplier, cod。产品,颜色,价格)
我想将table_2的数据插入到table_1,只有当table_2的id_supplier不存在于table1的id_supplier
我尝试了几个查询:
与
INSERT INTO table_1 (cod.products, color, price, id_supplier)
SELECT cod.products, color, price, id_supplier FROM table_2
我插入的所有东西都没有考虑到id_provider…
where where子句不起作用
INSERT INTO table_1 (cod.products, color, price, id_supplier)
SELECT cod.products, color, price, id_supplier FROM table_2
WHERE table_1.id_supplier <> table_1.id_supplier
WHERE table_1.id_supplier <> table_1.id_supplier
改变
WHERE table_2.id_supplier NOT IN (SELECT id_supplier FROM table_1 GROUP BY id_supplier)
使用NOT IN
运算符
INSERT INTO table_1 (cod.products, color, price, id_supplier)
SELECT cod.products, color, price, id_supplier
FROM table_2 where id_supplier not in (select distinct id_supplier from table_1)
;