我正在分享你的另一种选择。假设您在数据库中有记录,并且cid值为“1”。
如果值已存在于数据库中,请先检查该值。
$sql = "SELECT cid FROM phpc_events";
$returned = mysql_query($sql);
if(mysql_num_rows($returned) > 0){
while( $row = mysql_fetch_array($returned) ){
$PhpcArray [] = $row; //stores result returned in array to ensure less resource used
}
}
$sqlC = "SELECT anotherId FROM at_courses";
$returnedC = mysql_query($sqlC);
if(mysql_num_rows($returnedC) > 0){
while( $rowC = mysql_fetch_array($returnedC) ){
if( in_array( $rowC['anotherId'], $PhpcArray ) ){
// do nothing as id is already exists in phpc_events
}
else{
// do insertion because id in at_courses is not exist yet in phpc_events
}
}
}希望这可以帮助。
另一种选择。
顺便说一下,如果它与您的列匹配,您也可以尝试此查询,因为我不知道您的表结构是什么样的
SELECT cid
FROM at_courses
WHERE cid NOT IN (
SELECT cid FROM phpc_events
)此查询将返回at_courses中的cid,该cid尚未出现在phpc_events中。然后插入从at_courses表返回的那些cid。
谢谢。