我正在尝试使用以下代码将两个表的内容合并为一个表(sortedArrivals):我不希望插入主键ID(在这里称为flightShed.id)。

$sql = "INSERT INTO sortedArrivals

                SELECT  flightSched.id,
                        flightSched.timePeriod,
                        flightSched.depOrArriv,
                        flightSched.flightNo,
                        flightSched.airline,
                        flightSched.dest,
                        flightSched.origin,
                        flightSched.depature,
                        flightSched.don,
                        flightSched.arrivalTime,
                        flightSched.status

                FROM flightSched ";

        if (!$mysqli->query($sql))
          {
            echo $counter . "<br>" ;
            die('Error: ' . $mysqli->error);

          }


但是我想要一个递增的主键。当我尝试插入下一张表的内容时,出现以下错误消息:

错误:键“ PRIMARY”的条目“ 9”重复

我该如何实现?
任何帮助表示赞赏。

最佳答案

命名要插入的列

INSERT INTO sortedArrivals (timePeriod, depOrArriv, flightNo, airline,
                            dest, origin, depature, don, arrivalTime, status)
SELECT timePeriod,
       depOrArriv,
       flightNo,
       airline,
       dest,
       origin,
       depature,
       don,
       arrivalTime,
       status
FROM flightSched

09-12 16:04