Skip to content
Advertisement

Join two tables together with the use of a third table that contains composite keys from both?

I’m trying to combine two tables together with a third that is comprised of a single column that is a combination of the two tables primary key column. I’ve considered using substring to separate the column into parts that can be compared to either table’s key column but I might be making it more difficult.

Here's an example of what I'm working with

Advertisement

Answer

The problem here is your design; you need to fix it. You are storing a delimited value in your column, combinedcode, in your junction table.

What you should be doing is storing the 2 values in separate columns, and then creating your foreign keys on those values. This would look something like this:

Depending on your design, you may want to make it so that the value in the junction table are are unique:

Then, to do your JOINs it would be as simple as:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement