简体   繁体   English

如何将表的内容插入另一个表的内容中,但不包括其主键(id)?

[英]How do I INSERT the contents of a table into another, but excluding their Primary Key (id's)?

I am trying to merge contents of two tables into one (sortedArrivals) using the following code: I do not want the Primary Key id's (here called flightShed.id) to be inserted. 我试图使用以下代码将两个表的内容合并为一个表(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);

          }

However I would like a incrementing Primary Key. 但是我想要一个递增的主键。 When I try inserting the contents of the next table I get: the following error message: 当我尝试插入下一张表的内容时,出现以下错误消息:

Error: Duplicate entry '9' for key 'PRIMARY' 错误:键“ PRIMARY”的条目“ 9”重复

how do i achieve this? 我该如何实现? Any help is appreciated. 任何帮助表示赞赏。

Name the columns you want to insert into 命名要插入的列

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

I do not want the Primary Key id's (here called flightShed.id) to be inserted. 我不希望插入主键ID(在这里称为flightShed.id)。

Well then don't select them … 好吧,不要选择它们……

However I would like a incrementing Primary Key. 但是我想要一个递增的主键。

… but select just NULL instead in their place; …但是只选择NULL代替它们; a NULL value for an autoindex column will automatically increase the ai counter. 自动索引列的NULL值将自动增加ai计数器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将主键作为外键插入另一个表? - How do I insert the primary key to another table as foreign key? 如何将数据插入一个表并获取主键作为外键插入另一个表中? - How do I insert data into one table & get the primary key to insert in another table as a foreign key? 如何将行的主键插入其另一列? - How to insert a row's primary key in to another one of its columns? 如何将主键的值插入到插入查询中 - How do i insert the value of the primary key into a insert query 如何将主键放入另一个表的外键中? PHP,MySQL - How Do I Grab And Put Primary Key Into Foreign Key In Another Table? PHP, MySQL 如何获得另一个表的外键引用的主键? - How do I get the primary key being referenced by a foreign key of another table? SQL PHP如何基于另一个表的主键更新外键? - SQL PHP How do I update a foreign key based on the primary key of another table? 如何从两个表中查询匹配表中的外键和另一个表中的主键 - How do I query from two tables matching the foreign key from table with the primary key from another 如何同时将外键设置为与另一个表的主键相同? - How do I set the foreign key same as primary key of another table concurrently? 将主键插入具有2个单独插入的另一个表中 - Insert primary key into another table with 2 separate inserts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM