2013-01-05 2 views
0

추가 된 그림의 마지막 "날짜"를 표시하려고합니다 (특정 사용자와 관련된 ).여러 "ORDER BY"로 그룹화하는 방법

아래에 추가 요소를 입력하는 경우 ORDER BY :, timestamp DESC 결과에는 아무런 변화가 없습니다. 그룹마다 나는 첫 번째 사진의 날짜를 얻습니다.

어떤 사람들은 하나의 이미지를 가지고 있고, 다른 사람들은 이미지가 5 명 이상인 사람도 있습니다. 데이터베이스에 약 1500 명이 있습니다.

기타 모든 데이터/이미지가 올바르게 반환됩니다.

인터넷에서 내 문제의 참조를 찾을 수 없습니다. 더 일반화입니다.

감사

출력은 : -

Person's - Person - Nos Of - Last Picture 
Picture  Name Pictures  "Date" 
-------- ------ -------- ------------ 

image  Tony  5  26th June 2012 

image  Beth  1  6th January 2011 

image  John  2  5th May 2012 

msql_query은 다음과 같습니다 -

난 당신이 날짜에만 관심이 같은 문제가 주문 관련이 없습니다 생각
<?php 

    function person($person_id) { 

     $persons = array(); 

     $persons_query = mysql_query(" 
     SELECT `persons`.`person_id`, `persons`.`person_name`, 
     LEFT(`persons`.`person_description`, 50) as `person_description`, 
     `person_images`.`timestamp`, `person_images`.`image_person_id`, 
     `person_images`.`person_name`, `person_images`.`ext`, 
      COUNT(`person_images`.`image_person_id`) as `image_count` 
      FROM `persons` 
      LEFT JOIN `person_images` 
      ON `persons`.`person_id` = `person_images`.`person_id` 
     LEFT JOIN `person_images` 
     ON `persons`.`person_id` = `person_images`.`person_id` 
     WHERE `persons`.`member_id` = ".$_SESSION['member_id] 
     GROUP BY `persons`.`person_id` 
     ORDER BY `persons`.`person_name`, 
    "); 

    While ($persons_row = mysql_fetch_assoc($persons_query)) { 

     $persons[] = array(
      'person_id'    => $persons_row['person_id'], 
      'person_name'   => $persons_row['person_name'], 
      'person_description'  => $persons_row['person_description'], 
      'timestamp'    => $persons_row['timestamp'], 
      'image_person_id'   => $persons_row['image_person_id'], 
      'ext'      => $persons_row['ext'], 
      'count'     => $persons_row['image_count'] 
     ); 
    } 
    return $persons; 
    } 
?> 
+1

(http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) 일부 이전 질문 [동의], 그리고 난 –

+1

에 대한 대답을 생각해 볼 수 있습니다. http://stackoverflow.com/questions/1313120/retrieving-the-last-record-in-each-group – Angel

답변

2

가장 최근 사진의 그래서 대신이 방법을 시도에 대해 :

MAX(timestamp) GROUP BY person_id 
+0

이미지 ID를 원한다면 http://www.techonthenet.com/sql/max.php에서 실행 날짜를보고, 위의 내용을 귀하의 사례 - 타임 스탬프와 person_ id와 일치하는 다른 쿼리와 조인하여 image_id를 가져옵니다. –

+0

주문은 중요하지 않습니다. 따라서 MAX를 사용하면 작동하지 않습니다. 감사 – kevo

관련 문제