Hello,
I'm working with a modified version of tracker and I would like to be able to select out both the title of the post and the subject of the most recent comment on the post. The EASY thing to do would be to modify node_comment_statistics to include the cid of the last post (why isn't this in there already?!?), however I'm trying to do this without having to modify any tables. Here's the SQL I'm using right now. The problem is, the only way to limit the comments returned is by using MAX(c.cid), and unfortunately, while that returns the right cid, it doesn't return the right row for anything else in the comments table. In other words, c.cid may be 100 if 100 is the last comment in the thread, but if 1 was the first comment in the thread, the subject for 1 will be returned. And as far as I can tell, there's now way to tie MAX(c.cid) to the rest of the row.
Short of table modifications, anyone have a way to do this in a single SQL statement?
SELECT DISTINCT(n.nid), MAX(c.cid) AS cid, c.subject, n.title, n.type, n.changed, n.uid, u.name, cu.name as cname, l.last_comment_uid,
GREATEST(n.changed, l.last_comment_timestamp)
AS last_updated, l.comment_count
FROM node n, node_comment_statistics l
INNER JOIN users u ON n.uid = u.uid
INNER JOIN node_comment_statistics ON n.nid = l.nid
INNER JOIN users cu ON cu.uid = l.last_comment_uid