I am trying to filter a somewhat involved sqlite3 query using a pairwise association table. Say I have these tables (where pet_id_x references an id in table pets):
[pets]
id | name | animal_types_id | <additional_info>
1 Spike 2
2 Fluffy 1
3 Whiskers 1
4 Spot 2
5 Garth 2
6 Hamilton 3
7 Dingus 1
8 Scales 3
. . .
. . .
[animal_types]
id | type
1 cat
2 dog
3 lizard
[successful_pairings]
pet_id_1 | pet_id_2
1 4
2 4
2 8
3 2
3 4
4 5
4 6
4 7
5 6
5 7
6 7
. .
. .
A toy example for my query would be to get the names of all dogs which meet certain constraints (from columns within the pets table) and have > 2 successful pairings with other dogs, resulting in:
name | successful pairings
Spot 6
Garth 3
As per the above, the ids need to be combined from pet_id_1 and pet_id_2 in successful_pairings to get the total count of pairings involving each pet id.
I am new to sql syntax, and am having trouble chaining queries together to filter based on conditions distributed across multiple tables.
Aucun commentaire:
Enregistrer un commentaire