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 > conditional query question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-22-05, 18:48
videodead videodead is offline
Registered User
 
Join Date: Jan 2005
Posts: 2
conditional query question

I have the following table...

+----+-------------+----------+
| id | category_id | item_id |
+----+-------------+----------+
| 1 | 1 | 1 |
| 2 | 4 | 1 |
| 3 | 6 | 1 |
| 4 | 4 | 2 |
| 5 | 1 | 3 |
| 6 | 4 | 3 |
| 7 | 6 | 3 |
| 8 | 3 | 4 |
| 9 | 12 | 4 |
| 10 | 14 | 4 |
| 11 | 1 | 5 |
| 12 | 1 | 6 |
| 13 | 1 | 7 |
| 14 | 5 | 8 |
| 15 | 13 | 8 |
+----+-------------+----------+

I have a search that allows users to pick the categories they want to view items from but also to choose to not show items with other categories, is there any way in my query to stop an item from showing if it has both categories chosen, in other words, if the user wanted to view all the items from category 1 but not category 4 how could i make items 1 and 3 not show up in the results?
Reply With Quote
  #2 (permalink)  
Old 01-23-05, 00:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
select item_id
  from catitems
group
    by item_id
having sum(case when category_id = 1 then 1 else 0 end) > 0 
   and sum(case when category_id = 4 then 1 else 0 end) = 0
results: item_ids 5,6,7
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-23-05, 00:14
videodead videodead is offline
Registered User
 
Join Date: Jan 2005
Posts: 2
Works great, Thanks
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