如何在sql中比较和插入不同的表?

时间:2018-10-05 09:33:37

标签: sql sql-server

我有两个不同的数据库,即同一列。我想通过比较表之间的学生ID编号并将其具有相同的编号,从旧数据库复制到新数据库。

例如:

数据库名称1:StudentInformation,表名称:Student

StudentID     Image
---------------------
  123         1.png    
  142         2.png    
  175         3.png    
  475         4.png

数据库名称2:StudentInformation2,表名称:NewStudent

 StudentID       Image
 --------------------------
  145            a14.png    
  196            7.png    
  175            Null    
  875            9.png    
  475            Null 

两个表具有通用ID(ID = 175和ID = 475)

我希望得到此表:

新的StudentInformation3,表名:NewSameStudents

StudentID      Image
----------------------
  175          3.png    
  475          4.png    

7 个答案:

答案 0 :(得分:1)

这是一种方法:

SELECT s1.StudentID, s1.Image
FROM StudentInformation.[schema].student s1
WHERE EXISTS (SELECT 1 
              FROM StudentInformation2.[schema].NewStudent s2 
              WHERE s1.StudentID = s2.StudentID
             );

enter image description here

Demo

如果要使用上述选择的结果填充新表,则可以使用INSERT INTO ... SELECT构造:

INSERT INTO StudentInformation3.[schema].NewSameStudents (StudentID, Image)
SELECT s1.StudentID, s1.Image
FROM StudentInformation.[schema].student s1
WHERE EXISTS (SELECT 1 
              FROM StudentInformation2.[schema].NewStudent s2 
              WHERE s1.StudentID = s2.StudentID
             );

但是,我建议您不要这样做,因为您可以使用第一个查询创建一个(非实体化的)视图。另外,NewSameStudents是派生表,因此可能不得不经常更新,这可能很麻烦。

答案 1 :(得分:1)

您应该尝试使用SQL Server的Merge语句 Merge Statement Here

答案 2 :(得分:0)

使用内部联接和isert到select语句中以插入新表中

insert into NewSameStudents(studentid,image)    
    select a.studentid,a.image from Student a inner join newstudent n 
    on a.studentid=n.studentid
    where n.image is null

答案 3 :(得分:0)

下面是完全限定的SQL查询,它允许在数据库之间甚至不同数据库服务器之间选择数据。

  

SELECT * FROM [SERVER]。[DATABASE]。[SCHEMA]。[TABLE]

SELECT
  a.StudentID,
  b.Image
 FROM
   StudentInformation.Student a
 JOIN
   StudentInformation2.Student b
     ON a.StudentID = b.StudentID

答案 4 :(得分:0)

您的第三张表是否毫无意义

新的StudentInformation3,表名:NewSameStudents

StudentID      Image
----------------------
  175          3.png    
  475          4.png   

您可以使用inner join

SELECT StudentID,Image
FROM StudentInformation1
INNER JOIN StudentInformation2 
ON StudentInformation1.StudentID = StudentInformation1.StudentID;

答案 5 :(得分:0)

尝试一下:

    WITH StudentInformation AS (
    SELECT 123 AS StudentID, '1.png' AS Image UNION ALL
    SELECT 142, '2.png' UNION ALL
    SELECT 175, '3.png' UNION ALL
    SELECT 475, '4.png'
),
StudentInformation2 AS (
    SELECT 145 AS StudentID, 'a14.png' AS Image UNION ALL
    SELECT 196, '7.png' UNION ALL
    SELECT 175, NULL    UNION ALL
    SELECT 875, '9.png' UNION ALL
    SELECT 475, NULL
)

SELECT s1.StudentID, s1.Image
FROM StudentInformation2 s2 left join 
StudentInformation s1 ON s1.StudentID = S2.StudentID

答案 6 :(得分:0)

使用来自SQL的合并:

Merge into [TABLE_TARGET] as u
USING (Select * from TABLE) as c

on 
u.FIELD1 = c.FIELD2 ---JOIN
--------------------
WHEN MATCHED THEN
-------------------
Update SET
FIELD_FROM_TARGET = c.FIELD_FROM_TABLE

----------------------
WHEN NOT MATCHED THEN
----------------------
Insert (
FIELD_FROM_TARGET

) VALUES
(
FIELD_FROM_TABLE

);