Search code examples
mysqlsqlinner-join

How to join two MySQL tables based on the same column name?


I have 2 different tables. Both have several different columns, but there's a column serial_number that is in both these tables with duplicate serial numbers.

In the table 1, I want to append the row data of table 2 if the serial_number matches in both the tables.

Example:

Table 1

id, serial_number, name  , phone     , email
 1, 87454126     , Chris , 5105487451, [email protected]

Table 2

id, serial_number, status   , reason
 1, 87454126     , Completed, Some Reason

The result I want:

Table 1 changes to:

1, 87454126, Chris, 5105487451, [email protected], Completed, Some Reason

Kindly help me, I can't figure this out. Thanks


Solution

  • You can give both tables a different name and join like:

    SELECT *
    FROM Table1 AS t1
    JOIN Table2 AS t2 ON t1.serial_number = t2.serial_number;