2010-07-26 2 views
2

아래 입력은 "datesubmitted"라는 타임 스탬프 필드로 제출을 시간순으로 역순으로 정렬합니다. 이 필드는 "제출"이라는 MySQL 테이블에 있습니다.두 필드로 정렬

또 다른 MySQL 테이블 "comment"에는 "datecommented"라는 다른 타임 스탬프 필드가 있습니다.

각 제출에는 단 하나의 "datesubmitted"가 있지만 여러 개의 주석이있을 수 있으며 각 주석에는 "날짜가 서로 다릅니다."

"datesubmitted"및 각 "datecommented"의 제출을 ​​어떻게 정렬 할 수 있습니까? 즉,이 목록의 맨 위에 가장 최근에 제출 된 항목 또는 가장 최근에 작성한 항목이있는 항목 중 어느 것이 든 이 가장 최근에 인 항목을 표시하도록합니다. 사전에

감사합니다,

$sqlStr = "SELECT 
       s.loginid 
       ,s.title 
       ,s.url 
       ,s.displayurl 
       ,s.datesubmitted 
       ,l.username 
       ,s.submissionid 
       ,COUNT(c.commentid) countComments 
      FROM 
       submission s 
      INNER 
      JOIN 
       login l 
       ON 
       s.loginid = l.loginid 
      LEFT OUTER 
      JOIN 
       comment c 
       ON 
       s.submissionid = c.submissionid 
      GROUP 
       BY 
       s.submissionid 
      ORDER 
       BY 
       s.datesubmitted DESC 
      LIMIT 
       10";   

$tzFrom = new DateTimeZone('America/New_York'); 
$tzTo = new DateTimeZone('America/Phoenix'); 



// echo $dt->format(DATE_RFC822); 


$result = mysql_query($sqlStr); 

$arr = array(); 
echo "<table class=\"samplesrec\">"; 
while ($row = mysql_fetch_array($result)) { 
    $dt = new DateTime($row["datesubmitted"], $tzFrom); 
    $dt->setTimezone($tzTo); 
    echo '<tr>'; 
    echo '<td class="sitename1"><a href="http://www.'.$row["url"].'" TARGET="_blank">'.$row["title"].'</a> <div class="dispurl">'.$row["displayurl"].'</div></td>'; 
    echo '</tr>'; 
    echo '<tr>'; 
    echo '<td class="sitename2name">Submitted by <a href="http://www...com/.../members/index.php?profile='.$row["username"].'">'.$row["username"].'</a> on '.$dt->format('F j, Y &\nb\sp &\nb\sp g:i a').'</td>'; 

    echo '</tr>'; 
    echo '<tr>'; 
    echo '<td class="sitename2"><a href="http://www...com/.../comments/index.php?submission='.$row["title"].'&submissionid='.$row["submissionid"].'&url='.$row["url"].'&countcomments='.$row["countComments"].'&submittor='.$row["username"].'&submissiondate='.$row["datesubmitted"].'&dispurl='.$row["displayurl"].'">'.$row["countComments"].' comments</a></td>'; 
    echo '</tr>'; 
    } 
echo "</table>";  
+1

'LEFT'과 'OUTER'사이에 줄 바꿈이 없습니다. ;-) –

답변

4
SELECT s.loginid, s.title, s.url, s.displayurl, s.datesubmitted, l.username, 
    s.submissionid, COUNT(c.commentid) countComments, 
    GREATEST(s.datesubmitted, COALESCE(MAX(c.datecommented), s.datesubmitted)) AS most_recent 
FROM submission s 
INNER JOIN login l ON s.loginid = l.loginid 
LEFT OUTER JOIN comment c ON s.submissionid = c.submissionid 
GROUP BY s.submissionid 
ORDER BY most_recent DESC 
LIMIT 10 
+0

감사합니다, Bill Karwin. – John

0

쉼표로 구분하여 여러 열을 기준으로 주문할 수 있습니다. 그래서 당신은 할 수 ... ORDER BY s.datesubmitted DESC, c.datecommented DESC. 또한 둘 다 같은 방향 (오름차순/내림차순)이라면 끝 부분에서 한 번만 말할 수 있습니다. 쿼리는 목록의 첫 번째 열과 그 다음 열로 정렬되어 그룹별로 정렬됩니다.

2

더 높은 날짜에 따라 조건부로 ORDER BY을 원하는 것처럼 들립니다. 이것을 ORDER BY을 포함하십시오.

ORDER BY CASE WHEN datesubmitted > datecommented 
     THEN datesubmitted 
     ELSE datecommented END DESC 
+1

짧고, 우아하고 요점. – Kelsey

+0

나는 이것을 시도하고 그것은 작동하지 않았다. 하지만 내 노력에 감사드립니다. – John

관련 문제