query - distinct

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eyespark
Forum Commoner
Posts: 50
Joined: Tue Jan 24, 2006 7:36 am

query - distinct

Post by eyespark »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi

I have one more problem

I have 2 tables:

Articles with fields aid, title, intro, content, timestamp

and

Comments with fields commentid, aid, poster, content, timestamp

Now, I want to print out titles of recently commented articles (unique).

Here is what I have so far:

Code: Select all

<ul>
<? $result = mysql_query("SELECT DISTINCT aid FROM comments ORDER BY commentid DESC LIMIT 0, 10") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$caid = $row['aid'];
		$art= mysql_query("SELECT title FROM articles WHERE aid='$caid'");
		$clanek = mysql_fetch_row($art);
		$title= $art[0];
?>
	<li><a href="<? echo $row['aid']; ?>-a.html"><? echo stripslashes($title); ?></a></li>
	<? } ?>
</ul>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

SELECT
  `comments`.`aid`,
  `articles`.`title`
FROM
  `comments`
INNER JOIN
  `articles`
  ON
    `comments`.`aid` = `articles`.`aid`
GROUP BY
  `articles`.`title`
ORDER BY
  `comments`.`commentid`
:?:
Post Reply