|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
SitePoint Enthusiast
![]() Join Date: May 2004
Location: Salisbury, UK
Posts: 55
|
Help needed with "many-to-many" select statement
I have three tables:
The classifications table stores all of the differant attributes that a t-shirt might have. For example: Small, Medium, Large, Black, White, Blue, Pink, V-neck, Round neck, Short sleeved, Long sleeved etc. The junctions table sets up a many-to-many relationship between products and classifications. The idea is that any given t-shirt in the products table could have any number of the attributes in the classifications table. For example, one t-shirt could be available in two different sizes and three different colors. I want to set up a system so that the user can choose a range of classifications, and then filter the products table accordingly to show which t-shirts match their criteria. A typical requirement might be: Large and (White or Pink) and V-neck Any suggestions? I have everything else set up fine, I'm just struggling with formulating an SQL SELECT statement to do the query. Last edited by philwilks; Jul 8, 2005 at 11:55. |
|
|
|
|
|
#2 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Apr 2005
Location: Moscow, Russia
Posts: 559
|
Hi, maybe this will help:
Code:
SELECT p.prodName, p.description, p.price FROM products p INNER JOIN junctions j ON p.prodId = j.prodId INNER JOIN classifications c ON j.classId = c.classId WHERE c.className = 'Large' AND c.className = 'V-neck' AND ( c.className = 'White' OR c.className = 'Pink' ); |
|
|
|
|
|
#3 | |
|
SQL Consultant
![]() ![]() ![]() Join Date: Jul 2002
Location: Toronto, Canada
Posts: 31,026
|
Quote:
![]() |
|
|
|
|
|
|
#4 |
|
SQL Consultant
![]() ![]() ![]() Join Date: Jul 2002
Location: Toronto, Canada
Posts: 31,026
|
this should work for the given example (since presumably an item cannot be both white and pink) --
Code:
select p.prodName
, p.description
, p.price
from products p
inner
join junctions j
on p.prodId = j.prodId
inner
join classifications c
on j.classId = c.classId
group
by p.prodName
, p.description
, p.price
having sum(case when c.className
in ('Large','V-neck','White','Pink')
then 1 else 0 end)
= 3
|
|
|
|
|
|
#5 |
|
SitePoint Enthusiast
![]() Join Date: May 2004
Location: Salisbury, UK
Posts: 55
|
Thank you both for your advise!
Tangerine Dream: Rudy is right - your query will never return anything. This is exactly what I tried to begin with, before I posted on this forum. Rudy: I think you have grasped my problem - thank you very much for your example code! Unfortunately, I cannot assume that an item will not be both white and pink. For example, a "Bart Simpson" t-shirt might be available in Child, Small and Medium sizes, and in Blue, White or Pink. Any ideas?!? PS: I should have explained that I will be filtering the database using classId, not className. I only used the name in my question to make my problem easier to understand. Anyway, that's not a big deal. |
|
|
|
|
|
#6 |
|
SQL Consultant
![]() ![]() ![]() Join Date: Jul 2002
Location: Toronto, Canada
Posts: 31,026
|
okay, then
![]() Code:
having sum(case when c.className
in ('Large','V-neck')
then 1 else 0 end)
= 2
and sum(case when c.className
in ('White','Pink')
then 1 else 0 end)
> 0
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 18:22.











Hybrid Mode
