Solved

MySql and product filter

Posted on 2013-10-24
4
664 Views
Last Modified: 2013-10-25
I have products on an ecommerce website that have options eg size and colour. If I display a product filter (form) with checkboxes for all available attributes for each available option and the selected attributes are posted to some code that selects the product records then the sql below will fetch any product that has an options_values_id of 1 (eg blue) or 2 (eg red) where the options_values might be colours blue and red
select p.products_id, pd.products_name from products p left join products_attributes pa on p.products_id = pa.products_id where pa.options_values_id in(1,2);

Open in new window

The following code would do the same:
select p.products_id, pd.products_name from products p left join products_attributes pa on p.products_id = pa.products_id where (pa.options_id = 1 and pa.options_values_id = 1) or (pa.options_id = 1 and pa.options_values_id = 2);

Open in new window

But how could I search for products with colour red or blue and size medium where size is pa.options_id = 2 and medium is options_values_id = 3, thereby reducing results rather than increasing, without looping through all the
records and using an 'if' clause, can a union do this or a sub select?

It's difficult to present the full code and web page so if you need that then please don't comment.
0
Comment
Question by:ncw
  • 2
4 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 39597984
Short answer is that you'd have to do another left join on the same table. I'd have to see your table structure to be sure, but something like this:

select p.products_id, pd.products_name from products p
left join products_attributes pa on p.products_id = pa.products_id
left join products_attributes pa2 on p.products_id = pa2.products_id

where (pa.options_id = 1 and pa.options_values_id in (1,2))
  AND (pa2.options_id = 2 and pa2.options_values_id=3);
0
 
LVL 40

Expert Comment

by:lcohan
ID: 39597985
select p.products_id, pd.products_name
from products p left join products_attributes pa on p.products_id = pa.products_id
where pa.options_values_id in(1,2) and pa.options_id = 2;

or whatever value represents the correct size.

You need to create a Stored Procedure with parameters and pass in the values based on the UI selection(s)

http://forums.mysql.com/read.php?98,358569
0
 
LVL 24

Accepted Solution

by:
mankowitz earned 500 total points
ID: 39598021
A longer answer really depends on your table structure. The way you have it now, I can tell that the item exists in red, blue and green. I also know that the item exists in small, medium and large. What I can't tell is if the item exists as both small and red. Your database structure would not indicate that. See http://sqlfiddle.com/#!2/decdf/9

select p.products_id, 
  pa.products_name color, 
  pa2.products_name size 
from products p 
join products_attributes pa on p.products_id = pa.products_id 
join products_attributes pa2 on p.products_id = pa2.products_id 

where (pa.options_id = 1 and pa.options_values_id in (1,2))
  AND (pa2.options_id = 2 and pa2.options_values_id=3);

Open in new window

0
 
LVL 1

Author Closing Comment

by:ncw
ID: 39600150
Seems to work fine except I changed  
pa.options_id = 1 and pa.options_values_id in (1,2)
to
((pa.options_id = 1 and pa.options_values_id = 1) or (pa.options_id = 1 and pa.options_values_id = 2))
That fitted better when creating the sql dynamically depending on options selected.
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question