Guest Posted October 14, 2009 Share Posted October 14, 2009 I use this code to display the last posts from my phpbb forum $query = "SELECT post_subject, topic_id from phpbb_posts ORDER BY post_id DESC LIMIT 15"; $res = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($res)) { $title = substr($row["post_subject"], 0, 30); $topic_id = $row["topic_id"]; $post_id = $row["post_id"]; echo " <a href=\"viewtopic.php?f=5&t=$topic_id#p$post_id\" title=\"$title\">"; echo $title; echo "</a>..<br>"; } But if a topic has multiple new replys, it will list the topic multiple time. How can i remove the duplicate results? (keep only the last post of the topic) Exemple: Re: topic2 Re: topic1 Re: topic3 Re: topic1 Re: topic1 Re: topic4 Would be: Re: topic2 Re: topic1 Re: topic3 Re: topic4 thanks a lot! Link to comment https://forums.phpfreaks.com/topic/177631-remove-duplicates-in-a-query/ Share on other sites More sharing options...
ialsoagree Posted October 14, 2009 Share Posted October 14, 2009 Use "SELECT DISTINCT" instead of "SELECT". The distinct keyword tells MySQL not to include the same row more than once in the result set. Link to comment https://forums.phpfreaks.com/topic/177631-remove-duplicates-in-a-query/#findComment-936569 Share on other sites More sharing options...
MasterK Posted October 14, 2009 Share Posted October 14, 2009 I believe you just need to make it SELECT DISTINCT here.. $query = "SELECT post_subject, topic_id from phpbb_posts ORDER BY post_id DESC LIMIT 15"; So like this.. $query = "SELECT DISTINCT post_subject, topic_id from phpbb_posts ORDER BY post_id DESC LIMIT 15"; Link to comment https://forums.phpfreaks.com/topic/177631-remove-duplicates-in-a-query/#findComment-936570 Share on other sites More sharing options...
Guest Posted October 14, 2009 Share Posted October 14, 2009 doesn't work, maybe because there is 1 row for each reply but only 1 row for the topic..... i don't know, i'm really a newbie :/ Link to comment https://forums.phpfreaks.com/topic/177631-remove-duplicates-in-a-query/#findComment-936614 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.