If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Multiple IN() statements - is there a better way to group optional search IDs?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-17-11, 08:16
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
Multiple IN() statements - is there a better way to group optional search IDs?

I'm trying to build a query for pulling out wordpress posts based on multiple categories. The catgories are grouped into parent categories, ie:

Animals
- Fish
- Dog
- Cat

OS's
- Windows
- OSX
- Ubuntu

A user could select "Fish" and "Dog", and "OSX" (although God knows what they'd be looking for...) - the query would match anything that was tagged OSX, and either "Fish" OR "Dog"

The way I have it is multiple IN() statements, ie:

Code:
WHERE term_taxonomy.taxonomy = 'category'
AND (
	term_taxonomy.term_id IN(1,2)
	AND term_taxonomy.term_id IN(3,4)
)
However, there may be several groups of categories that I need to search for, and I'm a bit concerned that this isn't a very efficient way of doing it. Is there a better way?
Reply With Quote
  #2 (permalink)  
Old 01-17-11, 08:21
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
change this --
Code:
WHERE term_taxonomy.taxonomy = 'category'
AND (
	term_taxonomy.term_id IN(1,2)
	AND term_taxonomy.term_id IN(3,4)
)
to this --
Code:
WHERE term_taxonomy.taxonomy = 'category'
AND term_taxonomy.term_id IN(1,2,3,4)
although the performance improvement will be rather minuscule
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-17-11, 09:36
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
Sorry, I wasn't very clear. I need those groups of category ID's to be mandatory; it should match posts that have at least one of the grouped category ID's.

So IN(1,2,3,4) would match a post in any of those categories. I want it to match a post that's in either of categories 1 or 2, AND either of categories 3 or 4.

My concern about performance is that I might be chaining lots of those statements together, like:

term_taxonomy.term_id IN([list of categories])
AND term_taxonomy.term_id IN([another list of categories])
AND term_taxonomy.term_id IN([yet another list of categories])
etc, etc

If that's not actually going to be much of a performance issue - or not one I can do much about - then it's all academic anyway..
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On