pmiller624 Posted August 5, 2010 Share Posted August 5, 2010 I have three tables that when simplified are set up like below. tag_map +--------+--------+ | user_id | tag_id | +--------+--------+ | 1 | 1 | | 2 | 1 | | 2 | 2 | /*etc*/ tags +--+---------+ | id | name | +--+---------+ | 1 | PHP | | 2 | mySQL | /*etc*/ user +--+ | id | +--+ | 1 | | 2 | I have an sql query that looks like: SELECT * FROM tag_map JOIN `users` ON tag_map.user_id = users.id WHERE tag_id = 1 OR tag_id = 2 If you were to run this query on the data above user 2 would come up twice because he has the tags of 1 and 2. So my question is how can I make it so the query only selects a users id number once, no matter how many times his tag pops up? Quote Link to comment Share on other sites More sharing options...
abdfahim Posted August 6, 2010 Share Posted August 6, 2010 I am wondering, if you want to show every user only once, which corresponding tag_id you want to show? any one? maximum? or you dont want tag_id? Quote Link to comment Share on other sites More sharing options...
www333 Posted August 7, 2010 Share Posted August 7, 2010 SELECT distinct(user_id),tag_id FROM tag_map JOIN `users` ON tag_map.user_id = users.id WHERE tag_id = 1 OR tag_id = 2 You can " SELECT distinct(user_id)" not "tag_id",but it's meaningless. Quote Link to comment Share on other sites More sharing options...
dix.selken Posted August 8, 2010 Share Posted August 8, 2010 hello if you don't mind the value for tag you could use something like this SELECT user_id, max(tag_id) tag_id FROM tag_map JOIN `users` ON tag_map.user_id = users.id WHERE tag_id in (1,2) group by user_id Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.