473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Query to display names from multiple tables linked by ID's to a single table

Hi,

I am working on a plant database and I need some help with a query that i *thought* shouldn't have been too hard =\

I am trying to create a query that will display the names contained in 4 different tables but I am having trouble linking them using the main table containing ids

Here is the structure information for the 4 tables:
Families
Expand|Select|Wrap|Line Numbers
  1. family_id      int(10)               
  2. family_name     varchar(50) 
Genera
Expand|Select|Wrap|Line Numbers
  1. genus_id      int(10)     
  2. genus_name     varchar(50)
Species
Expand|Select|Wrap|Line Numbers
  1. species_id      int(10)       
  2. species_name     varchar(50) 
Infraspecies
Expand|Select|Wrap|Line Numbers
  1. infraspecies_id      int(10)        
  2. infraspecies_name     varchar(50)       
  3. rank     varchar(5)
Then the main table:
Plants
Expand|Select|Wrap|Line Numbers
  1. plant_id      int(10)       
  2. family_num     int(10)       families -> family_id       
  3. genus_num     int(10)      genera -> genus_id       
  4. species_num     int(10)     species -> species_id                 
  5. infraspecies_num     int(10)     infraspecies -> infraspecies_id
  6. syn_genus_num     int(10)     syn_genera -> syn_genus_id       
  7. syn_species_num     int(10)     syn_species -> syn_species_id       
  8. syn_infraspecies_num     int(10)     syn_infraspecies -> syn_infraspecies_id      
  9. scientific_name     varchar(100)             
  10. additional_info     text 
And I would like the query to print out the family name - genus name - species name - rank - infraspecies name (in that order). How exactly would i go about this, i was trying to do it using joins but that did not work.

Thanks in advance...
Oct 17 '07 #1
4 1942
ronverdonk
4,258 Expert 4TB
Welcome to TSDN!

Have you tried a query like the following (not quite correct column names, but the join is what you want)
Expand|Select|Wrap|Line Numbers
  1. SELECT p.plant_id, p.family_num, p.genus_num,p.species_num, p.infraspecies_num, g.genus_name, s.species_name, i.infraspecies_name, f.family_name 
  2. FROM  plants as p,generas g,families as f,species as s,infraspecies as i
  3. WHERE p.family_num=f.family_id AND
  4.       p.genus_num=g.genus_id AND
  5.       p.species_num=s.species_id AND
  6.       p.infraspecies_num=i.infraspecies_id; 
Ronald
Oct 17 '07 #2
Hey thanks for the tip, i tried the query and it worked but i edited it a bit to show just the fields i wanted. Problem is though that i would like null fields in infraspecies rank and infraspecies name to show up in the results so that i get the full amount of rows from plants table with all relevant information returned. How would i get that done, i'm assuming i would have to use joins somehow but i'm not exactly sure how to go about it.


Thanks
Oct 18 '07 #3
yaaaay, i think i got it lol, here's the query i used:
Expand|Select|Wrap|Line Numbers
  1. SELECT f.family_name, g.genus_name, s.species_name, i.rank, i.infraspecies_name
  2. FROM plants AS p
  3. LEFT JOIN (
  4. genera AS g, families AS f, species AS s
  5. ) ON ( p.family_num = f.family_id
  6. AND p.genus_num = g.genus_id
  7. AND p.species_num = s.species_id )
  8. LEFT JOIN infraspecies AS i ON p.infraspecies_num = i.infraspecies_id
  9.  
Output seems to be accurate so far plus it returned the correct number of rows from the plants table (2,393)
Oct 18 '07 #4
ronverdonk
4,258 Expert 4TB
I was just working on it, but you beat me to it.

Ronald
Oct 18 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Marizel | last post by:
I'm not sure there's an easy solution to this, but thought I'd ask. I often find myself with a query which I'd like to reuse, but with a different datasource. These datasources generally have...
1
by: kiqyou_vf | last post by:
Sorry, Google wouldn't let me post a reply. Here is the convo thus far: Jerry Stuckle wrote: > kiqyou_vf wrote: >> I'm trying to pull data from 2 different tables and do a loop to >> retrieve...
9
by: Jimbo | last post by:
Hello, I have a user request to build a form in an Access database where the user can check off specific fields to pull in a query. For example, let's say I have 10 fields in a table. The user...
10
by: Robert | last post by:
I am an attorney in a non-profit organization and a self-taught programmer. I'm trying to create a client db that will allow me to search for potential conflicts of interest based either on Social...
2
by: PL | last post by:
Hi, I have 2 simple tables, 'Accesses' and 'Partners'. Partners : - Id, primary numeric key - Name of the partner - other informational fields Accesses : - Id, primary numeric key
6
by: MVM | last post by:
Hi, I am attempting to run a query in MS SQL server between two tables that have a one to many relationship. The tables are linked on GID. What I want is one instance of every record from Table...
23
by: mlcampeau | last post by:
Hey guys, I have been mulling over this problem for a few days and have yet to come up with a query that will give me the expected results. I am working on a database that stores employee...
4
by: dreaken667 | last post by:
I have a MySQL database containing 16 tables of data. Each table has a different number of columns and there are few common field names accross tables. I do have one master table with which I connect...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.