Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old Nov 6, 2005, 03:50   #1
geosite
Non-Member
 
Join Date: Jan 2004
Location: Seattle
Posts: 4,500
Need Help with Multi-Table Query Involving Parents

Table gzecoregions (GZER) simply matches animal species with ecoregions. Table gWecoregions (GWER) lists all the ecoregions - AND their parents.

So the following query might display a list of all the ecoregions the wolf is native to:

PHP Code:

$area_res = mysql_query("SELECT DISTINCT GWER.ERName FROM gwecoregions as GWER

  INNER JOIN gzecoregions GZER ON GZER.Place = GWER.ERID
  WHERE GZER.Animal = '$Animal'"
);
such as...

Alaska Tundra
Canadian Taiga
Siberian Taiga

But I would like to also display the ecoregions' parents (biomes) and the biomes' parents (realms) as well. I discovered that I couldn't do it using just one table, so I created two separate tables for biomes and realms. But I can't figure out how to join them so that I can display something like this:

NEARCTIC (realm)
TUNDRA (biome)
Alaska Tundra
BOREAL FOREST (biome)
Canadian Taiga
PALEARCTIC (realm)
BOREAL FOREST (biome)
Siberian Taiga

It should be easy, but I'm doing something wrong. The following diagrams illustrate the relevant tables and fields...

PHP Code:

TABLE: gwecorealms

RealmID
| Realm | RealmParent
R
-NA | Nearctic | na
R
-NT | Neotropical | na
R
-NT | Neotropical | sa
R
-PA | Paleotropical | eur
R
-PA | Paleotropical | afr

TABLE
: gwecobiomes
BiomeID
| Biome | BiomeParent
B11
-NA | Tundra | R-NA
B11
-PA | Tundra | R-PA
B11
-AA | Tundra | R-AA
B11
-AN | Tundra | R-AN
B12
-NA | Desert | R-NA

TABLE
: gwecoregions
ERID
| ERName | ERParent
R
-NA | Nearctic | na
R
-NT | Neotropical | na
B11
-NA | Tundra | R-NA
B11
-PA | Tundra | R-PA
B11
-AA | Tundra | R-AA
NA1424
| Alaska Tundra | B11-NA
PA1340
| Siberian Tundra | B11-PA
Note that EVERY are, including all ecoregions, biomes and realms, is included in table gwecoregions, field ERName. So I display my data from that field.

Gwecoregions also has a field named ERParent, which includes the ID of each region's parent, so everything is listed in a parent-child relationship.

Below is a copy of my original query, followed by the query I used to join all four tables. Both queries work, but they produce the same results - a list of ecoregions only. How can I join them so my script also displays the parents?

Thanks.

PHP Code:

$area_res = mysql_query("SELECT DISTINCT GWER.ERName FROM gwecoregions as GWER

   INNER JOIN gzecoregions GZER ON GZER.Place = GWER.ERID
   WHERE GZER.Animal = '$Animal'"
);

$area_res = mysql_query("SELECT * FROM gwecoregions GWER
   INNER JOIN gzecoregions GZER ON GZER.Place = GWER.ERID
   INNER JOIN gwecobiomes GWEB ON GWEB.BiomeID = GWER.ERParent
   INNER JOIN gwecorealms GWERM ON GWERM.RealmID = GWEB.BiomeParent
   WHERE GZER.Animal = '$Animal' GROUP BY RealmN, BiomeN, ERN"
);
geosite is offline   Reply With Quote
 

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 01:22.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved