I’ve long been a MySQL addict, but I was told that PostgreSQL is a better choice for working with hierarchical queries - a very important feature for an animals website I’m working on. So I downloaded and installed PostgreSQL 9.5, which is running alongside MySQL right now.
Can anyone tell me how to write a hierarchical query, given the data in my table?
This is the schema from my table (as copied from pgAdmin III):
CREATE TABLE public.gz_life_mammals (
id integer NOT NULL,
taxon text NOT NULL,
parent text NOT NULL,
parent_id smallint NOT NULL,
slug text,
name_common text,
plural text,
extinct smallint NOT NULL,
rank smallint NOT NULL,
key smallint NOT NULL,
CONSTRAINT "Primary Key" PRIMARY KEY (id)
);
I’m just figuring out how to write PostgreSQL queries, but this is my first successful attempt…
$dbh = pg_connect("host=localhost dbname=geozoo user=postgres");
if (!$dbh) {
die("Error in connection: " . pg_last_error());
}
$sql = "SELECT * FROM gz_life_mammals";
$result = pg_query($dbh, $sql);
while ($row = pg_fetch_array($result)) {
echo "ID: " . $row[0] . " | ";
echo "Taxon: " . $row[1] . " | ";
echo "ParentID: " . $row[3] . "<br>";
}
// free memory
pg_free_result($result);
// close connection
pg_close($dbh);
The most important table fields for this exercise are the first four (id, taxon, parent and parent_id. The data looks like this:
ID | TAXON | PARENT | PARENT_ID
1 | Mammalia | Chordata | 1
2 | Carnivora | Mammalia | 2
3 | Felidae | Carnivora | 3
4 | Panthera | Felidae | 4
5 | Panthera-leo | Panthera | 5
6 | Panthera-tigris | Panthera | 5
Where the last two rows represent the lion (Panthera leo) and tiger (Panthera tigris). My ultimate goal is to be able to display both names and numbers (counts) of everything in the hierarchy. So if I navigate to MySite/panthera-leo, it displays “Class: Mammalia, Order: Carnivora.” And if I navigate to MySite/mammalia, it might display something like this:
Orders: 19
Families: 58
Genera: 688
Species: 8,034
The resources I’ve checked out so far have kind of thrown me for a loop, so maybe I should start by asking a simple question: Can this be done with one query, or will I need separate queries for extracting names and counting rows?
In fact, I’ve already figured out a way to use multiple “stairstep” queries to display grandparents and children with MySQL, so I’d really like to focus on counts. Can anyone show me how to write a PostgreSQL query that counts rows on various taxonomic levels?
I tried one script which I asked about @ Problem with PostgreSQL Query with Function (T_FUNCTION error) but it was too complex for me to figure out. I’d like to try and find either a simpler script or a different approach. Thanks.