我正在使用PostgreSQL来获取数据,我想将其插入MySQL。我有一个PHP脚本,它允许我获取数据,检查重复,然后将其插入MySQL DB。问题是,它不是仅插入非现有数据,而是插入一个随机行,而不插入任何其他行。
$check_for_episode = mysqli_query($conn, "SELECT DISTINCT house_number,number FROM episode WHERE house_number LIKE '".$house_number."' AND number = ".$episode_id_pg." LIMIT 1");
if (mysqli_fetch_array($check_for_episode)){
}else{
$insert_episode = mysqli_query($conn, "INSERT INTO episode(".$episode_col.")VALUES('".$episode_values."')");
}
变量$episode_col
和$episode_values
通过implode从名为$episode
的数组中获取数据:
$episode_col = implode(", ", array_keys($episode));
$episode_values = implode("', '", array_values($episode));
答案 0 :(得分:1)
这是你的代码。
IF
语句用于比较,你的语句只是获取数组,而IF
语句中没有任何条件。您需要修复语句以获取列及其各自的值。
$check_for_episode = mysqli_query($conn, "SELECT DISTINCT house_number,number FROM episode WHERE house_number LIKE '".$house_number."' AND number = ".$episode_id_pg." LIMIT 1");
if (mysqli_fetch_array($check_for_episode)){
}else{
$insert_episode = mysqli_query($conn, "INSERT INTO episode(".$episode_col.")VALUES('".$episode_values."')");
}
您也可以尝试使用WHERE EXISTS
或WHERE NOT EXISTS
。
SELECT DISTINCT column1 FROM table1
WHERE EXISTS (SELECT * FROM table2
WHERE table2.column1 = table1.column1);
SELECT DISTINCT column1 FROM table1
WHERE NOT EXISTS (SELECT * FROM table2
WHERE table2.column1 = table1.column1);
了解更多信息:https://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html