I’m starting to like MySQL

$result = mysql_query(“SELECT parent,MAX(id) as id,count(*) AS num FROM fft_comments group by parent ORDER BY id desc LIMIT 5″);

This query gets the data for the “Recent Comments” section on fft.askthefool.com. The point is to get the last five unique parent based comments and count the number of posts for that parent in the entire table.

After the data is extracted,

$row[0] = parent of the thread to generate the links (I use hierarchical origination)
$row[1] = not used for printing but is used to make sure you have the latest 5
$row[2] = number of posts for that thread

As simple as that. Amazing how tricky MySQL queries can extract complicated data without sacrificing time. Before I realized how to do complex queries I used to go ghetto stuff such as extract the last 400 rows and use a series of nested for and while loops to get a measly 5 rows of data.

# December 19th, 2002 @ 10:13am in