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 can I use AND in Select query on the same field of same table?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-09-08, 07:05
jitendramca jitendramca is offline
Registered User
 
Join Date: May 2008
Posts: 1
Question How can I use AND in Select query on the same field of same table?

I have one table "order_items", which contains user_id, product_id and some more fields. I want to found the users who have purchased three particular products having product_id e.g 101, 102, 103 (All Three Products not any of Three products)

Please help me...
How can I?


Jitendra
Reply With Quote
  #2 (permalink)  
Old 05-09-08, 07:40
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
use aliases

Id expect a query with 3 left joins would do the job, although there may well be a better solution...

Code:
select OI.User_ID, OI.Product_ID,........ from Order_Items as OI
left join order_items as A on A.User_ID=oi.User_ID
left join order_items as B on B.User_ID=oi.User_ID
left join order_items as C on C.User_ID=oi.User_ID
where A.Product_ID=101
and B.Product_ID=102
and C.Product_ID=103
and a.user_id != NULL
and b.user_id != NULL
and c.user_id != NULL
order by OI.User_ID
now whether it works or not I haven't got a clue.....

the left join should extract all records from OI, and those that match in the alisases A,B & C
where the product code is the required product code in A,B & C
AND where the use_ID in A, B & C is not NULL (ie there is something in the User_ID for each of the aliases, which we know if its present will be the same as the original table OI because of the join definition

it should work.. but it'll probably get slated by the knowledgeable ones here

you may need to check the not equals sign in MySQL,
likewise you may need to check if there is a better way of expressing comparisions to NULL using a function
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 05-09-08, 08:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT user_id
  FROM order_items
 WHERE product_id IN ( 101,102,103 )
GROUP
    BY user_id
HAVING COUNT(*) = 3
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 05-09-08, 08:58
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Rudy, your example hardly compares to healdem's NZDF classic.

Healdem, the != sign works fine in MySQL but only with non NULL values, you'd be best to use "is not NULL" here.
Reply With Quote
  #5 (permalink)  
Old 05-10-08, 18:24
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Rudy's is NZDF too!

...that is assuming one customer can order the same item on more than one order_items instance
__________________
George
Twitter | Blog
Reply With Quote
  #6 (permalink)  
Old 05-10-08, 18:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by georgev
Rudy's is NZDF too!

...that is assuming one customer can order the same item on more than one order_items instance
no it doesn't

all it assumes is that a customer has purchased those three items

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 05-10-08, 19:04
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
But customer 456 has bought product 101 on two separate occasions and 102 once - they will be included on the output.
__________________
George
Twitter | Blog
Reply With Quote
  #8 (permalink)  
Old 05-10-08, 19:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
oh, yes, of course :blush:

okay, HAVING COUNT(DISTINCT product_id) = 3
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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