I'm setting up a database to store laboratory analytical data:
I have a 'sample' table with a sample_id, sample_date, sample_location (when was the sample take, and from where)
I have a 'method' table with method_id, method_name and method_table_name (name of the analytical method and what database table represents the results)
I have an 'analysis' table with analysis_id, sample_id, method_id and analysis_date (which sample was analyzed by what method and when)
Then I share this primary key with one of many method tables. For example the 'atomic absorption' table will have atomic_absorption_id, atomic_absorption_copper, atomic absorption_iron to represent the amount of iron and copper in a sample. I also have a 'gc' table with gc_id and gc_ethanol
I can't figure out how to generate an SQL statement to join on a table who's name is the result of the query.
SELECT * from sample NATURAL INNER JOIN analysis NATURAL INNER JOIN method INNER JOIN (the value of method_table_name) ON analysis_id=(method_table_name)_id WHERE sample_id=2;
It might be easier to change the name of gc_id and atomic_absorption_id to both be analysis_id to enable a NATURAL INNER JOIN, but if multiple analyses were performed on one sample, I'll still have to add analysis.analysis_id='method_table_name'.analysis_id in the JOIN.
Any help is appreciate.
Kevin
I have a 'sample' table with a sample_id, sample_date, sample_location (when was the sample take, and from where)
I have a 'method' table with method_id, method_name and method_table_name (name of the analytical method and what database table represents the results)
I have an 'analysis' table with analysis_id, sample_id, method_id and analysis_date (which sample was analyzed by what method and when)
Then I share this primary key with one of many method tables. For example the 'atomic absorption' table will have atomic_absorption_id, atomic_absorption_copper, atomic absorption_iron to represent the amount of iron and copper in a sample. I also have a 'gc' table with gc_id and gc_ethanol
I can't figure out how to generate an SQL statement to join on a table who's name is the result of the query.
SELECT * from sample NATURAL INNER JOIN analysis NATURAL INNER JOIN method INNER JOIN (the value of method_table_name) ON analysis_id=(method_table_name)_id WHERE sample_id=2;
It might be easier to change the name of gc_id and atomic_absorption_id to both be analysis_id to enable a NATURAL INNER JOIN, but if multiple analyses were performed on one sample, I'll still have to add analysis.analysis_id='method_table_name'.analysis_id in the JOIN.
Any help is appreciate.
Kevin