2010-12-17 2 views
0

코멘트 수와 각 코멘트의 날짜와 함께 항목을 표시하고 싶습니다.SQL : 댓글 수와 최신 코멘트 날짜가있는 항목을 선택하십시오.

SELECT item.*, 
    count(comments.itemid) AS commentcount, 
    comments.created AS commentcreated 
FROM table_items AS item 
LEFT JOIN table_comments AS comments ON (comments.itemid = item.id) 
WHERE item.published=1 
GROUP BY item.id 
ORDER BY item.created DESC 

이제 commentcreated 값은 첫 번째 주석의 날짜입니다. 마지막 코멘트를 원합니다. LEFT JOIN을 (를) 통한 주문은 작동하지 않습니다. 이것이 어떻게 성취 될 수 있는가?

답변

2

SELECT item.*, 
    count(comments.itemid) AS commentcount, 
    max(comments.created) AS commentcreated 
FROM table_items AS item 
LEFT JOIN table_comments AS comments ON (comments.itemid = item.id) 
WHERE item.published=1 
GROUP BY item.id 
ORDER BY item.created DESC 

당신은 최대(), 분() 또는 다른 집계 함수 중 하나를 사용하는 그룹의 날짜의 어느 MySQL은 말할 필요 봅니다.

+0

예, 감사합니다. 이 작품! 그러나 최근 코멘트의 제목을 선택하려면 어떻게해야합니까? – Bert

+0

다른 질문이라고 생각합니다. – Jaydee

관련 문제