简体   繁体   中英

How to compare and insert different tables in sql?

I have two different databases, the same column.I want to copy from the old database to the new database by comparing the student ID numbers between the tables and having the same number.

For example:

Database name 1: StudentInformation , table name: Student

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

Database name 2: StudentInformation2 , table name: NewStudent

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

The two tables have common IDs (ID=175 and ID=475)

I want to get this table as a result:

New StudentInformation3 , table name: NewSameStudents

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

Here is one approach:

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

在此处输入图片说明

Demo

If you want to populate a new table using the results from the above select, then you may use the INSERT INTO ... SELECT construct:

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
             );

But, I might recommend against this, since you can just create a (non materialized) view using the first query. Also, NewSameStudents is a derived table, and therefore might have to updated frequently, which could be a hassle.

您应该尝试在此处使用SQL Server 合并语句的合并语句

Use inner join and isert into select statement for inserting in new table

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

Below is the fully qualified SQL Query where it allows to select data between databases and even between different database servers.

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

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

Your 3rd table make no sense if you mean

New StudentInformation3 , table name: NewSameStudents

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

You can use inner join

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

try this:

    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

Use Merge From 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

);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM