$threads = mysql_query("SELECT DISTINCT t.id, t.title
FROM threads t LEFT JOIN comments c ON t.id = c.parentID
ORDER BY c.date DESC");
while ($thread = mysql_fetch_assoc($threads)) {
echo "<li><a href=\"?threadID=$thread[id]\">".htmlspecialchars($thread['title'])."</a></li>\n";
}
Can anyone see a problem in this code? It wont work as i want it. I want it the thread that was commented in latest on top, and the the next latest etc.
echo $thread[parentID] echoes nothing
comments:
id, comment, parentID, date
You are ordering by a column not in your
DISTINCTlist.This is valid syntax in
MySQL, however thisORDER BYis meaningless.This syntax is a
MySQLextension, this query will fail in any otherRDBMSengine of the big four.It is used to simplify the
DISTINCTandGROUP BYqueries in the cases when same value ofcolumninSELECTorORDER BYalways corresponds to same value of column inGROUP BYorDISTINCT.If your case, you can have multiple values of
c.datefor each value oft.id, and which value ofc.datewill be selected forORDER BYis far from being guaranteed (it can be the last value, the first value or any other value).Rewrite your query as this: