Solved

MySql and product filter

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

19 Experts available now in Live!

Get 1:1 Help Now