无法获取数据库中每个模型名称的最后上传日期数据

Failed to fetch last date uploaded data in database for each model name

我有一个型号是dd1。该模型包括几种类型的模型名称。我想通过参考每个模型和每个模型名称的 last_datecreated 来获取模型的最后数据。

    $r= mysql_query("SELECT a.model_no, a.datecreated, a.model_name, a.model
                     FROM tbl_dsp_mail_inbox a 
                     INNER JOIN (SELECT model_name, MAX(datecreated) AS datecreated
                     FROM tbl_dsp_mail_inbox a 
                     WHERE status = 'sent' 
                     GROUP BY model_name
                     ) AS b ON a.model_name = b.model_name AND a.datecreated, b.datecreated");
     if(mysql_num_rows($r) !== 0){
         while ($data = mysql_fetch_object($r)){
         $model_n = $data->model_name;
         $datecreated = $data->datecreated; 
         $model_no = $data->model_no;

}

model  | model_name | model no | last_datecreated |
----------------------------------------------------
dd1    | toyota     | 0012-12-B| 25/12/2014
dd1    | kancil     | 0013-12-B| 25/12/2014
dd1    | toyota     | 0011-12-C| 24/12/2014
dd1    | toyota     | 018-13-C | 03/11/2014
dd1    | kancil     | 0014-12-E| 01/11/2014

But when I run this code the result is :

model | model_name | model_no | last_datecreated
--------------------------------------------------
dd1   | toyota     | 018-13-C | 25/12/2014
dd1   | kancil     | 0013-12-B| 25/12/2014

The should be like this (desired output)

model | model_name | model_no | last_datecreated
--------------------------------------------------
dd1   | toyota     | 0012-12-B | 25/12/2014
dd1   | kancil     | 0013-12-B | 25/12/2014

试试这个:

SELECT a.model_no, a.datecreated, a.model_name, a.model
FROM tbl_dsp_mail_inbox a 
INNER JOIN (SELECT model_name, MAX(datecreated) AS datecreated
            FROM tbl_dsp_mail_inbox a 
            WHERE status = 'sent' 
            GROUP BY model_name
           ) AS b ON a.model_name = b.model_name AND a.datecreated = b.datecreated;