SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old December 12th, 2003, 04:49 AM
nodger nodger is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: England
Posts: 7 nodger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Angry Beginners Question

Help . . . . A few days back I decided to teach myself SQL and set up an Access 97 database and ASP pages to try it all out. All going well, Inserted, selected, updated and deleted records without a problem.

However, I have now been struggling for 4 hours with what must be a basic problem . . .

I am trying to extract records from 2 tables with an AND relationship to the same field in multiple records in the second table. For example:

Table 1 - Contact
id
name

Table 2 - Fruit
contactid
Fruit

So a contact can have lots of different fruit he likes. If I want to extract all contacts who like Apples - no problem.

However how do I extract all contacts who like Apples AND Oranges. If I add a "WHERE Fruit = 'Apples' and Fruit = 'Oranges'" I get no records returned as I assume it is looking for both fields in the same record.

I realise this is basic level stuff, but I have struggled and cannot solve it!

Cheers

Nigel

Reply With Quote
  #2  
Old December 12th, 2003, 10:59 AM
Doug G Doug G is offline
Grumpier Old Moderator
ASP Free God 11th Plane (10000 - 10499 posts)
 
Join Date: Sep 2003
Posts: 10,143 Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 23 h 19 m 36 sec
Reputation Power: 181
Try "WHERE Fruit = 'Apples' OR Fruit = 'Oranges'"

Reply With Quote
  #3  
Old December 12th, 2003, 11:58 AM
nodger nodger is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: England
Posts: 7 nodger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi Doug

Thanks for the reply, but this does not seem to give the desired effect.

I am trying to extract the contacts who like both Apples and Oranges, the OR gives me contacts who like either Apples or Oranges.

Does this make sense?

Cheers

Nigel

Reply With Quote
  #4  
Old December 12th, 2003, 02:57 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
Then Try
Code:
"WHERE Fruit = 'Apples' AND Fruit = 'Oranges'"

Reply With Quote
  #5  
Old December 12th, 2003, 03:13 PM
nodger nodger is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: England
Posts: 7 nodger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi Memnoch

The and is where I started but it appears to be checking a single record not each record in the Fruit table - so never returns anything!

I am sure this must straightforward and I am just missing the point somewhere!

Thanks

Nigel

Reply With Quote
  #6  
Old December 12th, 2003, 06:15 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
try this
Code:
SELECT DISTINCT(A.name), B.Fruit
FROM Table1 As A, Table2 As B
WHERE (B.Fruit = 'Apples' OR B.Fruit = 'Oranges')
GROUP BY A.Name, B.Fruit

Reply With Quote
  #7  
Old December 12th, 2003, 06:46 PM
nodger nodger is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: England
Posts: 7 nodger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi again

Closer . . . first time I ran it listed all items in Table1 twice against both apples and oranges. I then added to WHERE "AND (A.id = B.contactid)" to link the 2 tables. This then gave me the contacts with Apples or Oranges again. IE

Alan Apples
Bill Apples
Bill Oranges
Dave Oranges
etc

Where all I really wanted was Bill! I guess I can sort this programmatically from here. I was only doing it as a learning exercise anyway! I think I have a bit of reading to do to work out what your SQL statements was doing!

Thanks for you help

Cheers

Nigel

Reply With Quote
  #8  
Old December 12th, 2003, 07:02 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
If Bill has both apples and oranges, then the query would return at least 2 rows.

Reply With Quote
  #9  
Old December 13th, 2003, 01:13 PM
nodger nodger is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: England
Posts: 7 nodger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks

As you say, I can programmatically check the Recordset for the number of matches required.

Out of interest, is this a restriction in SQL or bad design on behalf of my DB? I would of thought this sort of query would of been common place (IE what customer ordered a chair and a desk, what hotel accepts credit card and debit card, etc).

Sorry for all the questions, just trying to get an understanding of what can and cannot be done easily.

Cheers

Nigel

Reply With Quote
  #10  
Old December 13th, 2003, 02:21 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
Generally, the query would be something like this.

Let's assume that Bob had Apples and Oranges...The names are filled into a drop down list. When the user selects a user in the drop down list, the query below gets passed to another page.
Code:
SELECT Fruit FROM Table2 WHERE ID = " Request("ID")


So the page could display
Quote:
Bob
Apples
Oranges

Reply With Quote
  #11  
Old December 13th, 2003, 02:27 PM
nodger nodger is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: England
Posts: 7 nodger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi

Yes I can see that, but what I was trying to do was a search the other way round . . . . . IE
What contacts like apples and oranges - return Bob. (a bit like searching a CV for expertise - I want someone with SQL and VB).

Cheers

Reply With Quote
  #12  
Old December 13th, 2003, 05:35 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
Code:
SELECT DISTINCT(A.Name)
FROM Table1 As A
INNER JOIN Table2 As B On A.ID = B.ID
WHERE B.Fruit = 'Apples' OR B.Fruit = 'Oranges'

Reply With Quote
  #13  
Old December 14th, 2003, 11:07 AM
nodger nodger is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: England
Posts: 7 nodger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi

This still brings up everyone who likes apples or oranges. If I change the and to an or it then brings up nothing!! Arghhh!

Don't worry any further, as I say it was only a learning exercise anyway - I will return to the problem when I have more knowledge.

Thanks for your time, its appreciated.

Cheers

Nigel

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Beginners Question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |