【发布时间】:2020-09-25 14:55:26
【问题描述】:
最好检查一下产品是否已经导入到 MySQL 表中:
1) 每个产品 ID 的每个查询
foreach ($products_to_import_from_xml as $product) {
$is_exist = $modx->query("SELECT id FROM modx_ms2_products WHERE crm_id={$product['id']} LIMIT 1")->fetch(PDO::FETCH_COLUMN);
if ($is_exist) continue;
actually_import_new_product_because_it_not_exist_yet($product);
}
或
2) 使用“... WHERE `id` IN(array, of, all, importing, IDs)”?
$product_ids_to_import = [];
foreach ($products_to_import_from_xml as $product) {
$product_ids_to_import[] = $product['id'];
}
$existing_products = $modx->query('SELECT crm_id FROM modx_ms2_products WHERE crm_id IN ('.implode(',', $product_ids_to_import).')')->fetchAll(PDO::FETCH_COLUMN);
foreach ($products_to_import_from_xml as $product) {
$is_exist = in_array($product['id'], $existing_products);
if ($is_exist) continue;
actually_import_new_product_because_it_not_exist_yet($product);
}
【问题讨论】:
-
你是如何导入的,你想在什么时候检查?
-
请提供您的用例的最小可重现示例。事实上,您的问题缺乏上下文和精确性,并且不太可能得到有用的答案。
-
我从 XML 文件(从库存程序导出)将产品导入网上商店,并想检查每个出口的产品是否已经存在于网上商店产品表中。如果我选择第一种方式,我可以为每个产品签入
foreach,它是否已经存在并且不插入它并以这种方式检查每个下一个产品。或者我可以制作所有导出产品(我想导入)的数组列表,将它们传递给一个 MySQL 查询,通过这种方式获取所有已经导入的产品,当我在foreach循环导入产品时,检查每个产品存在于查询结果列表中 -
@Fullstack: 请edit your question 将信息放在那里而不是 cmets。
-
用代码示例编辑了我的问题(+ 添加标签 PHP)
标签: php mysql import duplicates sql-insert