简体   繁体   中英

Insert missing records from one table to another using mysql

I don't know why I am confused with this query.

I have two table: Table A with 900 records and Table B with 800 records. Both table need to contain the same data but there is some mismatch.

I need to write a mysql query to insert missing 100 records from Table A to Table B .

In the end, both Table A and Table B should be identical.

I do not want to truncate all the entries first and then do a insert from another table. So please any help is appreciated.

Thank you.

It is also possible to use LEFT OUTER JOIN for that. This will avoid subquery overhead (when system might execute subquery one time for each record of outer query) like in John Woo's answer, and will avoid doing unnecessary work overwriting already existing 800 records like in user2340435's one:

INSERT INTO b
SELECT a.* FROM a
LEFT OUTER JOIN b ON b.id = a.id
WHERE b.id IS NULL;

This will first select all rows from A and B tables including all columns from both tables, but for rows which exist in A and don't exist in B all columns for B table will be NULL . Then it filter only such latter rows ( WHERE b.id IS NULL ), and at last it inserts all these rows into B table.

I think you can use IN for this. ( this is a simpliplification of your query )

INSERT INTO table2 (id, name)
SELECT id, name
FROM table1
WHERE (id,name) NOT IN 
       (SELECT id, name
        FROM table2);

SQLFiddle Demo

AS you can see on the demonstration, table2 has only 1 records but after executing the query, 2 records were inserted on table2 .

如果它是 mysql 并且表是相同的,那么这应该有效:

REPLACE INTO table1 SELECT * FROM table2;

Code also works when a group by and having clauses are used. Tested SQL 2012 (11.0.5058) Tab1 is source with new records, Tab 2 is the destination to be updated. Tab 2 also has an Identity column. (Yes folks, real world is not as neat and clean as the lab assignments)

INSERT INTO Tab2
SELECT  a.T1,a.T2,a.T3,a.T4,a.Val1,a.Val2,a.Val3,a.Val4,-9,-9,-9,-9,MIN(hits) MinHit,MAX(hits) MaxHit,SUM(count) SumCnt, count(distinct(week)) WkCnt
FROM Tab1 a 
LEFT OUTER JOIN Tab2 b ON b.t1 = a.t1 and b.t2 = a.t2 and b.t3 = a.t3 and b.t4 = a.t4 and b.val1 = a.val1 and b.val2 = a.val2  and b.val3 = a.val3  and b.val4 = a.val4 
WHERE b.t1 IS NULL or b.Val1 is NULL
group by a.T1,a.T2,a.T3,a.T4,a.Val1,a.Val2,a.Val3,a.Val4 having MAX(returns)<4 and COUNT(distinct(week))>2 ;

This will insert the missing records into Table1

INSERT INTO Table2
(Col1, Col2....)
(
SELECT   Col1, Col2,... FROM Table1
EXCEPT
SELECT  Col1, Col2,... FROM Table2
)

You can then run an update query to match the records that differ.

UPDATE Table2
SET
Col1= T1.Col1,
Col2= T1.Col2,
FROM
Table T1
INNER JOIN
Table2 T2
ON
T1.Col1 = T2.Col1

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