sqlite - How to specify where clause with at least x values from nested select -


suppose have these tables:

person

id  name --  ---- 1   dude 2   john 3   doe ...etc 

favourite_food

personid   food --------  ------     1     apples     5     apples     5     oranges 

and want list of names of people @ least foods person 5 likes. below:

select p.name person p left join favourite_food ff on ff.personid = p.id ff.food = (select food favourite_food personid = 5) , ff.personid <> 5; 

except have no idea how specify 'at least' part. have create temporary table or so?

sql works sets, helps reformulate problem strictly in terms of set theory.

"at least" reformulated way: if @ foods favourite foods of person 5, persons have same number of favourite foods person 5?

select name person id in (select personid              favourite_food              food in (select food                             favourite_food                             personid = 5)              group personid              having count(food) = (select count(food)                                    favourite_food                                    personid = 5)             ) 

alternatively, use reformulation: not want persons not food person 5 likes. therefore, find persons no food exists liked person 5 not liked person:

select name person not exists (select 1                   favourite_food person5_food                   personid = 5                     , not exists (select 1                                     favourite_food                                     personid = person.id                                       , food = person5_food.food)                  ) 

(actually, sql based on relational algebra, , operation want called division.)


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -