Skip to content
Advertisement

INNER JOIN multiple columns to same column from another table

I got hmmd_prm table that is:

and lab_prm table that is:

and main_hmmd table that has names and pulls names using hmmd_id

I’m trying to get a result like this:

I am using something like this:

It is working as I wanted but I am pretty sure there is a way better solution to this.

How can I make this simplier?

Thanks in advance.

Update: What I am trying to achieve is, in the hmmd_prm table I have some materials as hmmd_id and user will apply some parameters to those parameters. I made 10 columns for parameters so user can assign max 10 parameters but might also assign 1 or 2 and rest null.

This might not be the best solution to this, since this is my first time doing something like this but I simply didn’t want the result showing as multiple entries of same material with different parameters in each row like below.

Advertisement

Answer

SQL and Relational Databases are opposite sides of the same coin, they both have normalised data structures baked in to their design.

This is just one sign that the structure your dealing with is not a good fit for SQL and so not a good fit for Relational Databases. (A code smell.)

As such, some repetition can be removed by normalising the structure on the fly, and then de-normalising again if you must.

Personally I’d advise not doing the GROUP BY and MAX(CASE) to de-normalise the results, and instead just use…

This will make many subsequent queries much simpler.

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