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? Link to comment https://forums.phpfreaks.com/topic/209930-only-select-if-column-value-is-unique/ 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? Link to comment https://forums.phpfreaks.com/topic/209930-only-select-if-column-value-is-unique/#findComment-1096019 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. Link to comment https://forums.phpfreaks.com/topic/209930-only-select-if-column-value-is-unique/#findComment-1096357 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 Link to comment https://forums.phpfreaks.com/topic/209930-only-select-if-column-value-is-unique/#findComment-1096477 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.