To merge tables with different tables with multiple primary keys

Viewed 16

enter image description here

I'm now trying to write query to make new table name 'want' with the data from table 'customer_tb' and 'study_history'.

For each table, primary key is colored in yellow.

I was trying to write query, but duplicate customer_id appears.

I wonder how I can get rid of duplicate values and make a table in the form of 'want' below.

1 Answers

In MySQL there is an option of setting an alias to a key or a table, that way you can use two different keys with similar names from two different tables and rename them so they do not clash with each other.

select id as id_A from tableA join select id as id_B from tableB

Here is documentation

You can also directly reference the key from the table you want to reference it to by using the table name, a period and then the key name like so:

tableA.id

If you are not using MySQL I am certain there are similar functionalities in other sql languages and servers.
Good Luck!

Related