简体   繁体   中英

How to “Group By” with multiple “ORDER BY”

I am trying to show the last "date" of a picture that was added, related to a specific person.

If I enter an additional element under ORDER BY: , timestamp DESC There is no change to the result. With every group I get the date of the first picture.

Some people have one image, others have twent five or more. There are about 1500 people in the database.

All other data/image is returned correctly.

I cannot find any reference on the internet to my problem, more a generalisation.

Thanks

The output is:-

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

The msql_query is:-

<?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;
   }
?>

I think your problem is not related to ordering as you are only interested in the date of the most recent picture. So how about you try this instead:

MAX(timestamp) GROUP BY person_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM