MYSQL Import Column from another table matching multiple columns

Tfom

I have two tables, one something like this:

Table 1:

ID- Name-   Code-   Code 2-

1-  John-   115-null    
2-  Rick-   652-null    
3-  Jones-  886-null    
4-  James-  554-null    
5-  Elton-  125-null    
6-  Craig-  214-null    
7-  John-   452-null    

Table 2:

Name-   Code-   Code 2- 

John-   115-    a   
Rick-   652-    b   
Jones-  886-    c   
James-  554-    d   
Elton-  125-    e   
Craig-  214-    f   
John-   452-    g   
Craig-  886-    h   
Rick-   115-    i   

This isn't the real data, it's not quite that simple. I need to get Code 2 from Table 2 into the Code # column in Table 1. To do this, I need to match up BOTH the Name and Code columns to get the data from Column 'Code 2' into Column 'Code #'. It needs to match against at least two columns as there are duplicates in each...

I want to end up with something like:

ID- Name-   Code-   Code 2-

1-  John-   115-a   
2-  Rick-   652-b   
3-  Jones-  886-c   
4-  James-  554-d   
5-  Elton-  125-e   
6-  Craig-  214-f   
7-  John-   452-g
Kritner

You can join tables on multiple columns at once like:

select t1.id, t1.name, t1.code, t2.code2
from t1
inner join t2 on t1.name = t2.name
    and t1.code = t2.code

this way (from your example) "John 115" will only be matched with "John 115" rather than "John 452", as the join is only being performed where both the name and code between the two tables are equal. (Note John 452 will also join to John 452).

You can build update statements based on selects if you were unaware. Your update statement would end up looking something like this:

update t1
inner join t2 on t1.name = t2.name and t1.code = t2.code
set t1.code2 = t2.code2;

This will join the two tables where ever name and code match, and will set code2 in the first table equal to code2 from the second table.

Here is an SQL Fiddle example.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MYSQL Import Column from another table matching multiple columns

From Dev

How to fetch all ids of a column by matching with multiple columns of another table?

From Dev

Mysql Query for inserting from one table to another on Multiple conditions and columns

From Dev

Mysql Query for inserting from one table to another on Multiple conditions and columns

From Dev

Replace certain records in a column of a table with another table by matching other columns

From Dev

How to get all columns from one table and only one column from another table with ID ? - MySql

From Dev

Import into mysql based on results from another table

From Dev

Mysql query to get rows of a table as columns of another, with column names from third table

From Dev

Delete rows in MySQL matching two columns in another table

From Dev

Delete rows in MySQL matching two columns in another table

From Dev

Import Column from CSV into existing MySQL Table

From Dev

Select multiple columns from a table and insert data into another table in a different database in PHP-MySQL

From Dev

Getting a value from another table for multiple columns

From Dev

Copy multiple columns from a table to another table with extra columns

From Dev

mysql: select rows from another table as columns

From Dev

MYSQL Query matching column names to rows in another table

From Dev

MySQL: Update rows in table, from rows with matching key in another table

From Dev

Update multiple columns in one table based on values in another table in mysql

From Dev

MySQL - Group by multiple columns from same table

From Dev

Reformatting MySQL table from Multiple Columns

From Dev

Copy Column Value from One table into Another Matching IDs

From Dev

Mysql insert column from another table

From Dev

mysql update an column from another table

From Dev

MySQL insert into column data from another table

From Dev

mysql update column value from another table

From Dev

MySQL update column from another table

From Dev

Copying One column from table to another table that has matching variables in another column

From Dev

Copying One column from table to another table that has matching variables in another column

From Dev

MySQL - Return matching text from a search over multiple columns

Related Related

  1. 1

    MYSQL Import Column from another table matching multiple columns

  2. 2

    How to fetch all ids of a column by matching with multiple columns of another table?

  3. 3

    Mysql Query for inserting from one table to another on Multiple conditions and columns

  4. 4

    Mysql Query for inserting from one table to another on Multiple conditions and columns

  5. 5

    Replace certain records in a column of a table with another table by matching other columns

  6. 6

    How to get all columns from one table and only one column from another table with ID ? - MySql

  7. 7

    Import into mysql based on results from another table

  8. 8

    Mysql query to get rows of a table as columns of another, with column names from third table

  9. 9

    Delete rows in MySQL matching two columns in another table

  10. 10

    Delete rows in MySQL matching two columns in another table

  11. 11

    Import Column from CSV into existing MySQL Table

  12. 12

    Select multiple columns from a table and insert data into another table in a different database in PHP-MySQL

  13. 13

    Getting a value from another table for multiple columns

  14. 14

    Copy multiple columns from a table to another table with extra columns

  15. 15

    mysql: select rows from another table as columns

  16. 16

    MYSQL Query matching column names to rows in another table

  17. 17

    MySQL: Update rows in table, from rows with matching key in another table

  18. 18

    Update multiple columns in one table based on values in another table in mysql

  19. 19

    MySQL - Group by multiple columns from same table

  20. 20

    Reformatting MySQL table from Multiple Columns

  21. 21

    Copy Column Value from One table into Another Matching IDs

  22. 22

    Mysql insert column from another table

  23. 23

    mysql update an column from another table

  24. 24

    MySQL insert into column data from another table

  25. 25

    mysql update column value from another table

  26. 26

    MySQL update column from another table

  27. 27

    Copying One column from table to another table that has matching variables in another column

  28. 28

    Copying One column from table to another table that has matching variables in another column

  29. 29

    MySQL - Return matching text from a search over multiple columns

HotTag

Archive