2009-08-07 3 views
2

이러한 테이블은 다음과 같습니다이 테이블을 어떻게 가입해야합니까?

threads: 
id, date, title, text 


comments: 
id, thread_id, date, comment 

어떻게 상단의 마지막 주석 스레드를 나열 할 것인가?

$threads = mysql_query("SELECT id, title FROM threads ORDER BY date ASC"); 

while ($thread = mysql_fetch_assoc($threads)) { 

echo $thread['title']; 

} 

나는 사람들을이 일을 알아낼 수 없습니다 :

은 어떻게 보이는지 현재. 그래서 누군가가 내게 큰 손길을 줄 수 있다면!

건배!

답변

5

이 시도 : 당신이 어떤 의견 스레드를해야하는 경우에

SELECT DISTINCT t.id, t.title 
    FROM threads t LEFT JOIN comments c ON t.id = c.thread_id 
ORDER BY c.date DESC 

왼쪽 필요 가입 할 수 있습니다. 당신은 상단의 최신 의견과 스레드의 목록을 원하기 때문에

+0

좋은 일. 나는 항상 GROUP BY로 그런 것들에 접근한다. +1 –

0
select * from Threads t 
    inner join Comments c 
     on t.id = c.Thread_id 
order by c.date 
0

, 댓글을 날짜별로 정렬 DESC 다음 ID로 가입해야 할 것이다.

SELECT t.id, t.title, c.date 
FROM threads t, comments c 
WHERE t.id = c.thread_id 
ORDER BY c.date DESC 

감사

이 하나가 완수해야
1

:

 
SELECT threads.id, threads.title, max(comments.date) 
    FROM threads 
    LEFT OUTER JOIN comments 
    ON threads.id = comments.thread_id 
GROUP BY threads.id, threads.title 
ORDER BY max(comments.date) DESC 
관련 문제