Whats wrong with my SQL query?
Posted
by William
on Stack Overflow
See other posts from Stack Overflow
or by William
Published on 2010-03-12T01:36:08Z
Indexed on
2010/03/12
1:47 UTC
Read the original article
Hit count: 239
sql
I'm trying to set up a query that shows the first post of each thread and is ordered by the date of the last post in each thread. I got the first part down with this query:
SELECT *
FROM (
SELECT Min( ID ) AS MinID
FROM test_posts
GROUP BY Thread
)tmin
JOIN test_posts ON test_posts.ID = tmin.MinID
Now I need to figure out how to call the last post of each thread into a table, than use that table to order the first tables results. So far I got this, but it doesn't work.
SELECT *
FROM (
SELECT Min( ID ) AS MinID
FROM test_posts
GROUP BY Thread
)tmin
JOIN test_posts ON test_posts.ID = tmin.MinID
ORDER BY (SELECT MAX( ID ) AS MaxID, Thread, MAX( Date )
FROM test_posts
GROUP BY Thread
)tmax
tmax.Date
© Stack Overflow or respective owner