473,385 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with SELECT id IN query

I need to build a query that queries products based on multiple categories.

A product can have multiple categories attached to it and I want to query products that are only in ALL of the selected categories.

My query might look like:

SELECT * FROM products INNER JOIN products_categories.productid = products.productid WHERE products_categories.categoryid IN (1,5,13)

The results I want is for the products that are returned would be in ALL three categories, 1, 5, and 13, however, that query would return products that were in ANY of those categories.

How can I alter this query to have the desired results? Thank you in advance!
Nov 6 '09 #1
4 2257
SLauren
60
Find the sum of all the selected values as you mentioned (1,5,13) and use the sum in the below query along with all the selected values:
// Assume total = 1 + 5 + 13 = 19
// First_Val = 1, Second_Val = 5 and Third_Val = 13

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM products WHERE products.productid IN (SELECT products_categories.productid FROM products_categories GROUP BY products_categories.productid HAVING SUM(IF(products_categories.categoryid = First_Val ,First_Val ,IF(products_categories.categoryid = Second_Val ,Second_Val ,IF(products_categories.categoryid = Third_Val ,Third_Val ,total + 1)))) = total);
I have placed the IF conditions inside the query as the sum of some other set of selected values may give the same total,e.g., 1 + 8 + 10 = 19....etc.

I am not sure at this point whether you want to include a product which contains a set of values like : 1,5,13,15,20....etc. Because this product also contains all the values which you have selected.If this is the case then you can change "total + 1" to 0 and then this query will result those products also.

Hope this could help.

Thanks,
Lauren
Nov 6 '09 #2
mwasif
802 Expert 512MB
I hope this will fulfill your requirement
Expand|Select|Wrap|Line Numbers
  1. SELECT *, COUNT(*) as cnt FROM products 
  2. INNER JOIN products_categories 
  3. ON products_categories.productid = products.productid
  4. WHERE products_categories.categoryid IN (1,5,13)
  5. GROUP BY products.productid
  6. HAVING cnt >= 3
Nov 9 '09 #3
SLauren
60
Thanks mwasif for providing this nice snippet :-).

Cheers,
Lauren....
Nov 10 '09 #4
Thanks everyone! I am going to give this a try and see how it works out.

Lance
Nov 11 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Surendra | last post by:
I have this query that I need to use in an Update statement to populate a field in the table by the value of Sq ---------------------------------------------------------------------------- Inline...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
2
by: Amanda | last post by:
From a guy in Microsoft newsgroups: | In *comp.databases.ibm-db2* there are always IBM guys | from the Toronto labs on line.Post with the | -for the love of god please help- | line...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
1
by: Melissa Kay Beeline | last post by:
OK, here's the sitch : we have an access control system at work that registers ever entry/exit of every employee. I recently made some queries in Access so the ppl in HR could make reports (who...
9
by: hope | last post by:
Hi Access 97 I'm lost on this code please can you help ================================= Below is some simple code that will concatenate a single field's value from multiple records into a...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.