2

我对 group by 的查询顺序有疑问。

我有一个表 download_category 并下载。下载表有一个名为“thumb”的列(它是一个图像 - varchar)。我想获取类别名称和插入的最后一个拇指。

我正在这样做:

    SELECT download_category.id
             , download_category.link
             , download.thumb
      FROM download  

RIGHT JOIN download_category ON (download_category.id = download.idCategoria )   
     WHERE download_category.link IN ('link-of-category') 

GROUP BY download_category.id
       , download_category.link
ORDER BY download.id DESC 

如果不存在下载,则需要 RIGHT JOIN。

问题是:拇指列返回第一个插入的记录,而不是最后一个“download.id DESC”。我究竟做错了什么?

4

1 回答 1

0

试试这个:

SELECT download_category.id
         , download_category.link
         , download.thumb
from download_category 
left join (select max(id) maxId,
                  idCategoria 
           from download 
           group by idCategoria) t
      ON (download_category.id = t.idCategoria ) 
left join download 
      ON t.maxid=download.id

 WHERE download_category.link IN ('link-of-category') 

 GROUP BY download_category.id
        , download_category.link
 order by t.maxid DESC;
于 2012-11-30T14:49:02.370 回答