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 > How to do this query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-03-10, 23:02
cy163 cy163 is offline
Registered User
 
Join Date: Apr 2007
Posts: 127
How to do this query

Hi ALL

THere is a data table from a flower shop which looks like
Code:
CustomerID        Flower

John                peony
John                lily
John                Lotus

Mary               peony
Mary               lily
Mary               chrysanthemum

Lisa                chrysanthemum
Lisa                peony
Lisa                kapok
I would like to find those customers' ID who buy the same n flowers. For example, in the above table, for peony and lily , the customers who buy both of them (n=2, for this case) are John and Mary.

I cannot figure out a SQL statement to do the above query. Please help.
Thanks
Reply With Quote
  #2 (permalink)  
Old 10-04-10, 03:27
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Hi,

if the column CustomerID and Flower is unique you could always use

Code:
SELECT CustomerID 
   FROM table 
  WHERE Flower IN ('peony','lily')
GROUP BY CustomerID
HAVING Count(*) = 2
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
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