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 > Data Access, Manipulation & Batch Languages > ASP > sql search from 2 table using checkboxes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-10-09, 21:14
iscode iscode is offline
Registered User
 
Join Date: Jan 2009
Posts: 3
sql search from 2 table using checkboxes

I have a form with checkboxes and I need to make a sql to collect all products (prodID) from table product who have same (OccID) as in table OccDetail.

First I use table Occasion to create the checkboxes with the OccName where each checkbox has it OccID. I click each checkbox and as I click it is saved in session and search for products who has this OccID and display them on the OnClick event.

1. Table
Occasion
----------
OccID
OccName

2. Table
Product
----------
ProID
ProName

3. Table
OccDetail
----------
OcDID
OccID
ProID

I need to know how I build the sql string to find all ProID in Product who has related ProID in OccDetail with the selected OccID.

Here is the sql I am using but its not working when more then 1 checkbox is selected :

SELECT Product.ProID, Product.ProName
FROM (Product INNER JOIN
OccDetail ON OccDetail.ProID = Product.ProID)
WHERE (OccDetail.OccID = 8) AND (OccDetail.OccID = 6)
Reply With Quote
  #2 (permalink)  
Old 01-11-09, 07:35
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Your where clause contradicts your join. Try OR instead of AND and see if that does what you want.
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 01-11-09, 17:57
iscode iscode is offline
Registered User
 
Join Date: Jan 2009
Posts: 3
thank you for your reply georgev
the reason I am using AND and not OR is I want only products that has both 6 and 8 in OccDetail. I dont want all products that has either 6 or 8
Maybe I didnt explain this clear enough sorry
Reply With Quote
  #4 (permalink)  
Old 01-12-09, 03:28
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Ok, then can you explain what about thhe query is not working when more than 1 checkbox is selected. Can we see the SQL that does work (i.e. only one selected) as well please.
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 01-12-09, 07:29
iscode iscode is offline
Registered User
 
Join Date: Jan 2009
Posts: 3
If I select only 1 checkbox I should get all products that has that OccID even if they have more OccID
and when I select more checkboxes I want to narrow the search and display products that has those OccID even if they have more OccID but restrict to have at least all OccID thats selected.

This works only for 1
SELECT Product.ProID, Product.ProName
FROM (Product INNER JOIN
OccDetail ON OccDetail.ProID = Product.ProID)
WHERE (OccDetail.OccID = 8)
Reply With Quote
  #6 (permalink)  
Old 01-12-09, 08:14
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
I'm still not 100% what you want to show, but maybe this will give you the result.
Code:
SELECT Product.ProID
     , Product.ProName
FROM   Product
 INNER
  JOIN (
        SELECT ProId
        FROM   occDetail
        WHERE  OccId IN (8, 12)
       ) As [hmm]
    ON Product.ProID = hmm.ProID
__________________
George
Twitter | Blog
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On