如何比较两个表,如果没有匹配,将这些值插入到没有数据的表中?

时间:2021-10-10 13:40:14

I need to compare two tables Main_Table and OUT-Table, OUT_Table contains data but Main_Table may not have all the data..so I need to compare these tables if no match then just insert these unmatched values to Main_Table. This is my query...

我需要比较两个表Main_Table和OUT-Table,OUT_Table包含数据,但Main_Table可能没有所有数据..所以我需要比较这些表,如果没有匹配,那么只需将这些不匹配的值插入Main_Table。这是我的查询......

Insert into Main_Table MT (MT.SerialNo) values (Select SerialNo 
from Main_Table MT where not exists (select SerialNo from OUT_Table OT 
where OT.SerialNo = MT.SerialNo))

This is selecting the values from out_Table which are not in Main_Table but not Inserting the values into main_table. I'am using Oracle database, Java(JSP). Please help me out to do this.

这是从out_Table中选择不在Main_Table中但不是将值插入main_table的值。我使用的是Oracle数据库Java(JSP)。请帮我解决这个问题。

1 个解决方案

#1


2  

Insert into Main_Table MT (MT.SerialNo)  
values ( Select SerialNo 
         from Main_Table MT 
         where not exists ( select SerialNo 
                            from OUT_Table OT 
                            where OT.SerialNo = MT.SerialNo
                          )
       )

The INSERT INTO..SELECT FROM syntax that you've used here isn't correct, the values keyword is not required here. Should be just

您在此处使用的INSERT INTO..SELECT FROM语法不正确,此处不需要values关键字。应该只是

Insert into Main_Table MT (MT.SerialNo)  
( Select SerialNo 
         from Main_Table MT 
         where not exists ( select SerialNo 
                            from OUT_Table OT 
                            where OT.SerialNo = MT.SerialNo
                          )
 )  

Here's an example that Oracle documentation provides.

以下是Oracle文档提供的示例。

#1


2  

Insert into Main_Table MT (MT.SerialNo)  
values ( Select SerialNo 
         from Main_Table MT 
         where not exists ( select SerialNo 
                            from OUT_Table OT 
                            where OT.SerialNo = MT.SerialNo
                          )
       )

The INSERT INTO..SELECT FROM syntax that you've used here isn't correct, the values keyword is not required here. Should be just

您在此处使用的INSERT INTO..SELECT FROM语法不正确,此处不需要values关键字。应该只是

Insert into Main_Table MT (MT.SerialNo)  
( Select SerialNo 
         from Main_Table MT 
         where not exists ( select SerialNo 
                            from OUT_Table OT 
                            where OT.SerialNo = MT.SerialNo
                          )
 )  

Here's an example that Oracle documentation provides.

以下是Oracle文档提供的示例。