linkin Posted October 29, 2006 Share Posted October 29, 2006 ok basicly I have a table, which contain three columns:ID | ID2 | ID3--------------1 5 62 3 83 3 74 3 95 25 18--------------the inputs will be ID3 (e.g. 7,9,18) i.e multiple inputs, not just onethe output needs to be: which is the most popular ID2, based on how many ID3 shared. sorted by popularity.e.g. input of 7,9,8,18 will result in 3 and then 25all i need is the SQL query, if you can help i would appreciate itif you need more explanation, tell me.thanks in advance Link to comment https://forums.phpfreaks.com/topic/25470-this-is-quite-a-challenge-but-any-help-is-appreciated/ Share on other sites More sharing options...
toplay Posted October 29, 2006 Share Posted October 29, 2006 If I understand you correctly, then a query like this will get you the result you want:SELECT id2 , COUNT(id2) AS id2_countFROM ids_tableWHERE id3 IN (7, 9, 8, 18)GROUP BY id2ORDER BY id2_count DESC # most popular first;Outputs something like:id2 id2_count--- ----------3 325 1 When you want to only select the most popular that have at least 2 or more entries, then use the HAVING clause, as in this example:SELECT id2 , COUNT(id2) AS id2_countFROM ids_tableWHERE id3 IN (7, 9, 8, 18)GROUP BY id2HAVING id2_count > 1 # At least 2 or more hitsORDER BY id2_count DESC # most popular first;Which would only return:id2 id2_count--- ----------3 3Notice it would not return the 25 in this example. Link to comment https://forums.phpfreaks.com/topic/25470-this-is-quite-a-challenge-but-any-help-is-appreciated/#findComment-116333 Share on other sites More sharing options...
linkin Posted October 29, 2006 Author Share Posted October 29, 2006 pure genius :D ...thank you so much Link to comment https://forums.phpfreaks.com/topic/25470-this-is-quite-a-challenge-but-any-help-is-appreciated/#findComment-116347 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.