Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Query from tables sharing primary keys

Query from tables sharing primary keys

Query from tables sharing primary keys

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.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close