Jump to content

remove duplicates in a query


Guest

Recommended Posts

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

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"; 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.