Matching specific one-to-many entries with SQL -


suppose have 2 tables:

product ------- productid name 

and

language -------- productid code 

i have 3 products:

1, 'homebody' -> 1, en 2, 'continetnal' -> 2, fr                  -> 2, en                  -> 2, es 3, 'westy'       -> 3, en                  -> 3, es                  -> 3, fr                  -> 3, pt 4, 'oktoberfest' -> 4, de 

suppose have list of languages interested en, fr, es in create 3 types of query select number of products.

  1. match language in list ('homebody', 'continental', 'westy')
  2. match languages in list ('continental', 'westy')
  3. match languages in list ('continental')

i think solution first just:

select * products p join language l on (p.productid = l.productid) l.code in ('en', 'es', 'fr') 

what way me solve other queries? suspect count number of languages associated product , make sure equal-to or at-least number of languages in query, not sure how make happen.

if can't have 2 times fr (or language) same product :

2.

select p.productid, p.name  products p  join language l on (p.productid = l.productid)  l.code in ('en', 'es', 'fr') group p.productid, p.name having count(*) = 3 

3.

where l.code in ('en', 'es', 'fr') , not exists (select null language productid = p.productid    , code not in ('en', 'es', 'fr') group p.productid, p.name having count(*) = 3 

or if wanna retrieve languages codes

select * products p join language l on p.productid = l.productid p.productid in   (select p1.productid product p1    join language l1 on (p1.productid = l1.productid)      l1.code in ('en', 'es', 'fr')     group p1.productid,     having count(*) >= 3) 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -