I have one table that I need to pull data from to display. there are two fields that a user's id can be attached to. Field1 and Field2, but they are always either or. In addition, I have a couple of extra where clauses in my query. My experience in the past is that you cannot have

Where field1= user_id
or where field 2= user_id
Order by field 3;

Can I try this:

where field 1or field 2 = user_id
order by field 3.

I am not aware if this is possible. If so, can you show me how?

If not, I think I have create two queries. One that gets field 1in the where and the other with field 2. Then I would combine the two arrays, then sort by field 3.

I think this way is the way I am gonna haveto go. My problem is that I am very weak in working with arrays. Can someone please help me put the two mysql_results into 1 array and then sort it? I will probably need assistance in retrieving the data. I know how to do it with:
while (mysql_fetch array())

If you have any questions or need more info, let me know.

Can you provide details of the table structure and the actual query you're using? I cannot see any reason why what you're asking cannot be achieved using MySQL alone.


table name: data1

SELECT * FROM data1 WHERE fk_primary_user_id = "23" or fk_shared_user_id = "23"
AND field_1 = "x" and field_2 = "y" ORDER BY field_1;

The user can be either a primary or shared owner of the record. I want to get all of the ones the user is attached to, and then sort.

If this seems correct, then I need to look further into what might be causing erroneous records to show up.

Does that help?


That does look fine. The one change I would make would be to wrap the where or conditions in brackets to ensure they're being interpretted correctly. E.g:

FROM data1 
WHERE (fk_primary_user_id = 23 OR fk_shared_user_id = 23)
AND field_1 = 'x' 
AND field_2 = 'y' 
ORDER BY field_1;

Does this change the result you get at all? If not, can you give more detail as to what erroneous results you're getting? What makes them incorrect?


yes, that worked. Thank you very much