473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

should be easy query question

Tim
hi all,

I have a table of customers.

I have a table of products they have ordered.

How can I find all customers who have ordered productA and productB at
any time.

It sounds so easy, but I can't quite get my head around it!

thanks

Tim

Aug 18 '05 #1
7 1291
Jim
In article <11**********************@g44g2000cwa.googlegroups .com>, Tim wrote:
hi all,

I have a table of customers.

I have a table of products they have ordered.

How can I find all customers who have ordered productA and productB at
any time.


Do you mean productA AND productB, or productA OR productB?

In other words, do you want clients who have ordered both products, or
all customer who have ordered either product?

One's easy, the other is...less so.

Jim
--
Find me at http://www.ursaMinorBeta.co.uk
"There's no 'I' in team, but there is a 'me' if you jumble
the letters up a bit." - Dr. House.
Aug 18 '05 #2
Tim
not surprisingly, it's the difficult option I'm after where I want to
return all customers who have bought productA and productB.

thanks

Aug 18 '05 #3
Jim
In article <11**********************@z14g2000cwz.googlegroups .com>, Tim wrote:
not surprisingly, it's the difficult option I'm after where I want to
return all customers who have bought productA and productB.


Try something like:

select custid from table where product='ProductA' and custid in
(select custid from table where product='ProductB') order by custid

Can't promise it will work but a few tests of the results should prove
it one way or the other.

Jim
--
Find me at http://www.ursaMinorBeta.co.uk
"There's no 'I' in team, but there is a 'me' if you jumble
the letters up a bit." - Dr. House.
Aug 18 '05 #4
Stu
Without your table structures, this is only a guess;

SELECT *
FROM Customers
WHERE CustomerID IN (SELECT a.CustomerID
FROM (SELECT *
FROM products
WHERE p.Type = 'A') a
JOIN
(SELECT *
FROM products
WHERE p.Type = 'B') b
ON a.CustomerID = b.CustomerID)

is one way to do it.

Stu

Aug 18 '05 #5
Tim
yes I can see that would work. thanks.
but just for fun, how would that look if I wanted to compare 3, 4 or
even 10 products?!
does it just turn into a nasty mess?

I was half wondering whether to run a script now and then to flatten
the tables into a new table to string all products purchased into 1
field, and use the instring function to find what I'm looking for.

Aug 18 '05 #6
On 18 Aug 2005 08:27:30 -0700, Tim wrote:
hi all,

I have a table of customers.

I have a table of products they have ordered.

How can I find all customers who have ordered productA and productB at
any time.

It sounds so easy, but I can't quite get my head around it!

thanks

Tim


Hi Tim,

Here's a version that's both shorter and more expandable than Jim's
suggestion:

SELECT Customer
FROM Orders
WHERE Product IN ('A', 'B')
GROUP BY Customer
HAVING COUNT(*) = 2

Easy to expand to three, four, ... products. And if you ahve the
products you want to search for in a seperate table, the query changes
to one of the following two (they are equivalent, but might have
different execution plans - test both if speed matters)

SELECT Customer
FROM Orders
WHERE Product IN (SELECT Product FROM ProductsToFind)
GROUP BY Customer
HAVING COUNT(*) = (SELECT COUNT(*) FROM ProductsToFind)

SELECT Customer
FROM Orders
WHERE EXISTS
(SELECT *
FROM ProductsToFind
WHERE ProductsToFind.Product = Orders.Product)
GROUP BY Customer
HAVING COUNT(*) = (SELECT COUNT(*) FROM ProductsToFind)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Aug 18 '05 #7
Stu
Hugo,

That's awesome. I knew there had to be a better way to do that than my
lousy suggestion, and that's really clean.

Stu

Aug 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: | last post by:
I don't know SQL at all, but I have a problem now because I must use SQL in my PHP scripts. So please help me! I have 2 tables: id | name ----------- 1 | thing1 2 | thing2 3 | thing3 4 ...
3
by: Andy Visniewski | last post by:
Should be easy, but I've been trying to figure this out for about half an hour with no luck. There is a table 'Cybex' which has all the Cybex products we sell, and a table 'Datasheets' which...
4
by: Andy Proctor | last post by:
I hope there is an answer out there.... I have a simple database structured like this (non relevant tables and fields omitted) Members table memberID memberFname memberLname memberNokID
36
by: No Spam | last post by:
Dear fellow Access 2003 Users, Is there a way to trim all of the fields in a table in one swoop using VBA (preferred) or a query? Right now, I am using an update query and updating EACH field...
2
by: Salsa_Dancer | last post by:
Hi forum, I have this constructor inside a class that derives from webcontrol public example() { for(int i=0;int<num;i++) { //MyGroup is an array of TextBoxes Property inside
5
by: LedZep | last post by:
What up, All I need to do is enter a last name in a text box, query a MSAccess database and display the name with the corresponding columns. This is no problem, but when there are more than one...
8
by: bsimmons01 | last post by:
Hi All, I'm completely green to DB2, so please pardon my ignorance if this is an extremely easy question. I'm converting some queries in our application and it's choking on the Group-By...
3
by: Chad Dokmanovich | last post by:
As someone who's had a "fair" amount of web development experience, I shocked that I can't answer this question myself: Q: How does one maintain state and pass data across web pages and do so in...
4
by: n | last post by:
Hello! Here is a problem I hope you can point me to a solution. It Problem: A teacher needs to know which lesson to teach. A school has a curriculum with 26 lessons, A-Z. For a given class,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.