Solved

MySql and product filter

Posted on 2013-10-24
4
611 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 39

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now