简体   繁体   English

仅将选择的值从一个表复制到另一个表,并附带条件

[英]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.

相关问题 使用“ where”条件将值从一个表复制到另一个表 - Copy values from one table to another with 'where' condition 根据另一个表的选定输出更新另一个表中的行,但程序只更新顶部一行 - To update rows in another table based on selected output from another table, but program only update top one row 如何将表从一个数据库复制到另一个数据库? - How to copy table from one database to another? 将选定的行从一个jsp表传递到另一个jsp表 - Passing selected rows from one jsp table to another jsp table 从一个表读取行并将其复制到Java中的另一个数据库表 - Reading row from one table and copy it to another table of database in Java 如何将数据从一个表复制到 Firebase 中的另一个表? - How to copy data from one table to another table in Firebase? 查看一个表中出现在另一表中的值 - see values from one table that appear on another table 如何将表值从数据库复制到另一个数据库 - How to copy table values from a database to another database 一个表中的SQL副本插入另一个表中-使用where子句 - SQL copy from one table insert into another - using a where clause 我可以从另一个数据库复制一个数据库中的表吗? - Can I copy a table in one databsae from another database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM