简体   繁体   中英

Counting siblings in parent-child query

I have a database table filled with information on animals, where the field 'Taxon' contains scientific names and 'Parent' lists each taxon's parent. For example, the values for the polar bear look like this, where Ursus maritimus is the polar bear's scientific name:

TAXON | PARENT
Mammalia | Vertebrata
Carnivora | Mammalia
Ursidae | Carnivora
Ursus | Ursidae
Ursus-maritimus | Ursus

This is a simplified example of my query (where $MyURL = a page's URL, eg MySite/life/ursus-maritimus):

$stm = $pdo->prepare("SELECT L.Taxon, L.Parent
 FROM gz_life L
 WHERE L.Taxon = :MyURL");
$stm->execute(array(
 'MyURL'=>$MyURL,
));

while ($row = $stm->fetch())
{
 $Taxon = $row['Taxon'];
 $Parent = $row['Parent'];
}

So I can easily display a taxon's parent. I've also figured out how to display grandparents and children. Can anyone tell me how I can display the number of a taxon's sister groups (and perhaps its parent's sister groups)?

For example, I think there are seven species in the bear family. So if I'm visiting one of their pages (MySite/life/ursus-maritimus), I'd like a script that says the polar bear has six sister taxa (or it is one of seven taxa).

It would also be cool to know how to determine whether its parent (Ursus) is the only genus in the family (Ursidae). Or, if not, how many other genera are in the family? But if that's too complicated, ignore it, and I'll tackle that by itself later.

Right now, I'd just like to know how to tell my web page that the polar bear is one of seven species in its family, while the aardvark is the only species in its genus.

Note: I've received two great suggestions, but neither one is working for me yet. I edited this post to show my attempts.

Solution #1

When I echo $Sibling, it displays "Ursus" instead of all the siblings.

$stm = $pdo->prepare("select gp.Taxon Sibling, count(*) - 1
from gz_life g
inner join gz_life gp on g.Parent = gp.Parent
where gp.Taxon = 'Ursus'
group by g.Parent");
 $stm->execute(array(
 ));

while ($row = $stm->fetch())
{
 $Siblings = $row['Sibling'];
}

Solution #2

When I echo $Taxon2, it displays "Ursus" instead of all the siblings.

$stm = $pdo->prepare("SELECT L.Taxon, L.Parent, (SELECT COUNT(*)
FROM  gz_life AS cnt WHERE cnt.Parent = L.Taxon)
from gz_life L
  WHERE L.Taxon = 'Ursus'");
 $stm->execute(array(
  'MyURL'=>$MyURL,
 ));

while ($row = $stm->fetch())
{
 $Taxon2 = $row['Taxon'];
}

You can count the number of siblings each taxon has, with this:

select gp.taxon, count(*) - 1 siblings
from gz_life g
inner join gz_life gp on g.parent = gp.parent
where gp.taxon = 'Ursus'
group by g.parent

This query will display only one row, showing the number of taxons below the parent of a particular taxon. The - 1 will exclude the taxon in question, showing you the number of siblings.

Demo at: http://sqlfiddle.com/#!9/91e35/4

About the second part, (to determine whether its parent is the only genus in the family), I'm not sure if I understood correctly, but couldn't you use the same technique, but this time using the parent in the where condition? I think it's the same situation.

You can get the number of species in the genus in your main/original query, but to go all the way up the chain I think you'll need some recursion logic in your application code. But for the species in the genus, just add a subquery into what you have as a third column:

SELECT L.Taxon, L.Parent, (SELECT GROUP_CONCAT(siblings.Taxon) FROM gz_life AS siblings WHERE siblings.Parent = gz_life.Parent AND siblings.Taxon != gz_life.Taxon) AS Siblings ...

Then $row['Siblings'] will be a comma separated (string) list of the siblings. str_getcsv($row['Siblings']) would then give you an array of them for easy counting/iterating/whatever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM