Find what group a data belongs to based on query result

时间:2018-09-18 19:56:45

标签: sql database oracle

So lets say in this case, the group that we have is groups of animals.

Lets say I have the following tables:

animal_id | attribute_id | animal
----------------------------------
 1        |  1           | dog   
 1        |  4           | dog
 2        |  1           | cat
 2        |  3           | cat
 3        |  2           | fish
 3        |  5           | fish


id | attribute
------------------
 1 | four legs 
 2 | no legs
 3 | feline
 4 | canine
 5 | aquatic

Where the first table contains the attributes that define an animal, and the second table keeps track of what each attribute is. Now lets say that we run a query on some data and get the following result table:

attribute_id 
------------
     1
     4

This data would describe a dog, since it is the only animal_id that has both attributes 1 and 4. I want to be able to somehow get the animal_id (which in this case would be 1) based on the third table, which is essentially a table that has already been generated that contains the attributes of an animal.

The third table that has 1 and 4 doesn't have to be 1 and 4. It could return 2 and 5 (for fish), or 1 and 3 (cat).

We can assume that it's result will always match one animal completely, but we don't know which one.

0 个答案:

没有答案