tags:

views:

25

answers:

4

suppose i have 1 current user id and another user id to which current user is visiting.....than i want to fetch mysql data only if both have same options.....

for example user1 has uploaded a picture and user2 has also uploaded picture.......than how can i matchd user1 to user2 and query should be like this........

select * from users where id='user1id' or id='user2id' and imguploaded='1'

is this query is correct or not but it is not working for me..........!! i want a working query same as above not as select * from users where imguploaded=1

A: 

try ecapsultating your where with brackets, because of the precedence...

select * from users where (id='user1id' or id='user2id') and imguploaded='1'

if you don't mysql will presume default precedence and interpret the query like this:

select * from users where id='user1id' or (id='user2id' and imguploaded='1')

which will not give the desired result.

To check if both users have actually have a picture uploaded you could make it:

select COUNT(*) as count from users where (id='user1id' or id='user2id') and imguploaded='1'

and then check if count==2

Nicky De Maeyer
This will work if EITHER of the users has an uploaded image. I think the OP is asking for a query that will tell him if BOTH users have an image uploaded.
Alex
A: 

Why would you want such a thing like this?

Anyway... create some sort of actions table where you have a structure like id, action, desc Then create a user_action table like id, user_id, action_id. Then fetch your results.

This way you store data in therefore meant tables and don't mess up userdata with their action data. This way you can also easily extend actions and compare users of their made actions.

Ben Fransen
A: 
select * from users where (id='user1id' and imguploaded='1') and (id='user2id'and imguploaded='1')
Colour Blend
thanks boss i got that
testkhan
Did it help you?
Colour Blend
A: 

How about with a join?

query = "SELECT * FROM users AS first JOIN users AS second ON first.imguploaded = second.imguploaded WHERE first.userid = $user1 AND second.userid = $user2"

This query would take the two users, and if they have the same attribute (imguploaded is the same for both), return both rows, you can then select what you need. You can add more attributes to the ON clause, for example: ON first.imguploaded = second.imguploaded OR ( first.imgdloaded = second.imgdloaded AND first.somethingelseuploaded = second.somethingelseuploaded).

This way (with the ON clause of the mysql statement) you can combine all the attributes in the AND/ON but you have to place the brackets - ( and ) - in the right places. Of course, if you don't put them, the MySQL server will just read them serially.

Is that what you need?

zladuric