Let's say, I have two tables, "entities" and "tags". Relation between them is expressed by third table "entity_tags", which have tho fields: entity_id and tag_id. And I want to select all entities sorted by it's relevance to the given set of tags. For example, there is "banana", "orange", "potato", "pen" and "car" as entities and I want to filter all the entities that related to at least 2 of tags "fruit", "organic" and "item". What is the best way to do it?
First thing to come in mind is to use query like
select entity.*
, count(*) as relevance
from entities
, tags
, entity_tags
where entities.id=entity_tags.entity_id
and tags.id=entity_tags.tag_id
and tags.name in ('fruit', 'organic', 'item')
group by entity.id
order by relevance
but it isn't possible to add and relevance > 1
to where
, sqlite is reporting about "misuse of aggregate".
Result should look like this:
1|banana|3
2|orange|3
3|potato|2
And, by the way, is it possible to avoid nested select in counting of such rows?
select count(*)
from (<previous select>)