我想在更改列call_list_row的同时从表listid中复制行,但是如果没有其他包含listid的行已经具有该特定。这是我必须复制行并更改$new_list_id的SQL:$sql= "INSERT INTO call_list_row (listid,candid,date,byuser,status,emailed) SELECT '".$new_list_id."',candid,date,byuser,status,emailed FROM call_list_row WHERE listid='".$list."'";我该如何修改它,以使表中没有具有相同candid的新listid的行? 最佳答案 在组合键listid, candid上创建唯一索引:CREATE UNIQUE INDEX x_listid_candid ON call_list_row(listid, candid);然后,您可以使用INSERT IGNORE,这样,如果表中已经存在具有该组合的行,则INSERT将被跳过而不会出现错误。$sql= "INSERT IGNORE INTO call_list_row (listid,candid,date,byuser,status,emailed) SELECT '".$new_list_id."',candid,date,byuser,status,emailed FROM call_list_row WHERE listid='".$list."'";如果您无法创建此索引(也许允许重复,但是您不想在此过程中创建任何新的重复项),则可以签入查询。$sql= "INSERT IGNORE INTO call_list_row (listid,candid,date,byuser,status,emailed) SELECT '".$new_list_id."',candid,date,byuser,status,emailed FROM call_list_row AS c1 WHERE listid='".$list."' AND NOT EXISTS ( SELECT * FROM call_list_row AS c2 WHERE listid = '$new_list_id' AND c1.candid = c2.candid)";关于php - 复制行,同时忽略基于特定列的重复项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35281172/ 10-11 05:01