[英]Copy only selected values from one table to another table with condition
I am trying to copy selected values from rows of one table to another table, the problem is that SQLite gives an error for a nested SELECT
command as 我试图将所选值从一个表的行复制到另一张表,问题是SQLite为嵌套的
SELECT
命令提供了一个错误,如下所示:
java.sql.SQLException: only a single result allowed for a SELECT that is part of an expression
Here is what I am trying: 这是我正在尝试的:
INSERT INTO table2(ID, ProjectName )
SELECT ID, ProjectName FROM table1
Where table1.ID NOT IN table2
I can't use *
here, since table1
has four columns and table2
has only 3. All has to be done is to check if any ID
value from table1
is not present in table2
, then copy only that ID
, respective ProjectName
values from table1
and insert it into table2 as ID, ProjectName , null
我不能在这里使用
*
,因为table1
有四列,而table2
只有3列。所有要做的就是检查table2
是否不存在table1
任何ID
值,然后仅复制该ID
以及table1
相应ProjectName
值并将其作为ID, ProjectName , null
插入到table2中ID, ProjectName , null
null is for the thrid column value in table2. null是table2中第三列的值。
Any suggestions or help would be great 任何建议或帮助都很好
Your query is almost there: 您的查询几乎存在:
INSERT INTO table2(ID, ProjectName )
SELECT ID, ProjectName
FROM table1
Where table1.ID NOT IN (select table2.id from table2);
This is standard SQL, so it should work in any database. 这是标准的SQL,因此它可以在任何数据库中工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.