简体   繁体   中英

How to insert rows from table A into table B that are not in table B

I'm trying to find rows in one table (Table A) that do not exist in another (Table B), and then insert those rows into the that other table (Table B). However, there are multiple duplicates of rows in both tables, and I want to count new additional duplicates as new rows to be inserted.

The current structure is similar to this:

Table A:

+--------+-------+---------+
| prefix | fname |  lname  |
+--------+-------+---------+
| Mr     | John  | Smith   |
| Mrs    | Jane  | Doe     |
| Mr     | John  | Smith   |
| Mrs    | Jane  | Doe     |
| Mr     | Bob   | Jones   |
| Mrs    | Jane  | Doe     |
| Mr     | John  | Smith   |
| Mrs    | Sally | Johnson |
| Mr     | Bob   | Jones   |
| Mrs    | Alice | Apples  |
+--------+-------+---------+

Table B: (id is auto-incrementing)

+----+--------+-------+---------+
| id | prefix | fname |  lname  |
+----+--------+-------+---------+
|  1 | Mr     | John  | Smith   |
|  2 | Mr     | John  | Smith   |
|  3 | Mrs    | Jane  | Doe     |
|  4 | Mr     | Bob   | Jones   |
|  5 | Mrs    | Sally | Johnson |
|  6 | Mrs    | Sally | Johnson |
+----+--------+-------+---------+

The set of NEW additions (rows in Table A that are not in Table B) would be the following:

+--------+-------+--------+
| prefix | fname | lname  |
+--------+-------+--------+
| Mrs    | Jane  | Doe    |
| Mrs    | Jane  | Doe    |
| Mr     | John  | Smith  |
| Mrs    | Alice | Apples |
+--------+-------+--------+

Note: There are now fewer entries for "Mrs Sally Johnson", but I'm only concerned about times when the number of identical rows increases , not when it decreases .

After inserting these into Table B, allowing the id to auto-increment, Table B would look like this:

+----+--------+-------+---------+
| id | prefix | fname |  lname  |
+----+--------+-------+---------+
|  1 | Mr     | John  | Smith   |
|  2 | Mr     | John  | Smith   |
|  3 | Mrs    | Jane  | Doe     |
|  4 | Mr     | Bob   | Jones   |
|  5 | Mrs    | Sally | Johnson |
|  6 | Mrs    | Sally | Johnson |
|  7 | Mrs    | Jane  | Doe     |
|  8 | Mrs    | Jane  | Doe     |
|  9 | Mr     | John  | Smith   |
| 10 | Mrs    | Alice | Apples  |
+----+--------+-------+---------+

I've looked at the different Joins, as well as Unions and Intersects. However, I'm concerned that these solutions will not take into account the number of duplicate rows. IE, for this example I would expect them to only add "Mrs Alice Apples".

You could try something like this:

INSERT INTO TableB
SELECT Distinct prefix,fname,lname
FROM TableA 

The SELECT DISTINCT statement is used to return only distinct (different) values.

Credit to @JohnFx for the answer

you can get the missing rows uisng left join annd checking for not matching rows using is null

select a.prefix, a.fname,  a.lname
from tableA a 
left join tableB b  on a.prefix = b.prefix
    AND a.fname = b.fname 
        AND a.lname = b.lname
WHERE b.finame is null

you can insert the missing rows using a insert select

insert into tableB (prefix, fname, lname)
select a.prefix, a.fname,  a.lname
from tableA a 
left join tableB b  on a.prefix = b.prefix
    AND a.fname = b.fname 
        AND a.lname = b.lname
WHERE b.finame is null

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