I have a table of custom data (called fields)
field_id, user_id, field_value
lets just say: row 1
field_id=1
user_id=2
field_value ='aaa'
row 2
field_id=2
user_id=2
field_value = 'bbb'
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')
WHERE (f.field_id=1 AND f.field_value = 'aaa') OR (f.field_id=2 AND f.field_value = 'bbb')
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
so I would only want to get user 1 ebcause they amtch both 'aaa' and 'bbb', but user 2 does not...
you want to fetch all those users (userid) who have both the values 'aaa' and 'bbb'. right.
i'm not sure wat are trying to achieve thru field_id in this case..
YOu should create a view that is a join on this table w/ itself to see all pairs of attributes for the same user. Then compare that view to your user. You can't analyze two rows - its illogical. When you create the view, it'll have 1 user with both attributes, then you can see who has both and if your user has it.
Basically, you are trying to find a user that has a particular 2 rows of data. Normally, you can't do that, you only do row by row comparisons. In order to emulate this, you need to create a view - where it will show the user, column_a, column_b, the 2nd column_a and the second column_b. You know what a view is, right?
You then check that the first field is 'aaa' and the second is 'bbb' and it should work.
Bookmarks