To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here

HOME News MS SQL Oracle DB2 Access MySQL PHP Scripts Books Links DBA Talk


Go Back   Database Journal Forums > Related Sites > SQL Course

SQL Course SQL Course > Ask questions about the lessons on SQL Course 1 and 2. If you have problems > with the interface, please post in the Feedback forum

Reply Post New Thread
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 02-04-2003, 02:52 PM
JFLund JFLund is offline
Junior Member
 
Join Date: Feb 2003
Location: Salt Lake City, UT
Posts: 1
AND Joins

This is probably simple but I'll be darned if I can figure out how to do it...

I have a couple of tables linked by a CustomerID. The first table is of customers with the CustomerID field, the second is of items purchased by the customers. This Purchases table has a field for the ItemID and a CustomerID.

I want to create a query that will tell me which customers have purchased items with ItemID = 1 AND ItemID = 2 (and possibly Item = 3...) IE. I only want customers that have purchased BOTH or ALL the items, not just one of the items.

Any ideas?

Thanks
John F. Lund
Reply With Quote
  #2  
Old 02-05-2003, 10:26 AM
hpeter hpeter is offline
Junior Member
 
Join Date: Dec 2002
Posts: 5
try sth like:

SELECT CustomerID
FROM tbl1 a, tbl2 b
WHERE a.CustomerID= b.CustomerID
HAVING count(*) >=2
GROUP BY a.CustomerID
Reply With Quote
  #3  
Old 02-05-2003, 02:33 PM
JBane JBane is offline
Registered User
 
Join Date: Dec 2002
Posts: 181
John,

For 1 and 2:

select customerID from purchases t1 where
itemID = 1 and exists (select customerID from purchases t2
where t2.itemID = 2 and t1.customerID = t2.customerID)


For 1, 2, and 3:


select customerID from purchases t1 where
itemID = 1 and exists (select customerID from purchases t2
where t2.itemID = 2 and t1.customerID = t2.customerID)
and exists (select customerID from purchases t3
where t3.itemID = 3 and t1.customerID = t3.customerID)



Jeff
Reply With Quote
Reply Post New Thread

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 05:21 AM.


DatabaseJournal Recent Articles


 » Preparing To Upgrade Access Tables to SQL ...

 » Microsoft Windows PowerShell and SQL Serve...

 » New MySQL Enterprise with Query Analyzer B...

 » Quest Software Strengthens Committment to ...

 » Oracle Unveils New Event-Driven Middleware...

Search Database Journal:
 





Acceptable Use Policy

JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers

Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.