Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Old 02-17-2006, 12:50 AM Problem with query
Experienced Talker

Posts: 41
Trades: 0
I have a table of custom data (called fields)

lets just say:
table users
id, email
1, a@b.c
2, d@e.f

table fields
field_id, userid, value
1,1,'aaa'
2,1,'bbb'
1,2,'aaa'
2,2,'ccc'

I am having problems with the WHERE part of my clause.. I want to select all rows where the user has field_id[1] a value of 'aaa' and also has a field_value[2] of 'bbb'...

WHERE (f.field_id=1 AND f.field_value = 'aaa') AND (f.field_id=2 AND f.field_value = 'bbb')

hope someone can help me make it work, thanks!!


The full query:
SELECT u.* FROM users AS u, fields AS f WHERE (f.field_id=1 AND f.field_value = 'aaa') AND (f.field_id=2 AND f.field_value = 'bbb')

so I would only want to get user 1 because they match both 'aaa' and 'bbb', but user 2 does not...

thankss!
Triexa is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-17-2006, 08:14 AM Re: Problem with query
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
'aaa') OR (f.field =
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-17-2006, 01:37 PM Re: Problem with query
Experienced Talker

Posts: 41
Trades: 0
the thing is, I want to grap all users who have BOTH fields matching their respective values...?
Triexa is offline
Reply With Quote
View Public Profile
 
Old 02-18-2006, 01:14 PM Re: Problem with query
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Then use OR

AND means that all the conditions have to be met OR means any of the conditions need to match.

in your example above the two conditions in paranthesis cannot both be true at the same time.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Problem with query
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.20130 seconds with 12 queries