Mysql Group By Last Slno(最新版)

时间:2013-08-06 12:56:09

标签: php mysql group-by

我想通过最后的Slno获得价值分组

这是我的表

slno  title       date_added  comp_name  cat
----  ----------  ----------  ---------  ----
   1  title1.0    1jan2012    AAA        cat1
   2  title 2.5   3jan2012    AAA        cat1
   3  title 30.2  5jan2012    AAA        cat1
   4  title 41.2  9jan2012    AAA        cat1

这是我的查询

SELECT slno,title,date_added
from files where cat = :cat 
GROUP by comp_name
Order By comp_name DESC limit 12

,输出

title1.0**1(slno)

我想获得输出

title 41.2**4(slno)

请帮忙

已编辑...

确定只允许按comp_name和最后一个slno ...

进行分组

1 个答案:

答案 0 :(得分:1)

类似的东西:

SELECT slno,title,date_added
from files,
  (select max(slno) as mslno,comp_name from files where cat=:cat group by comp_name) t1
 where cat = :cat 
   and slno=mslno
   and files.comp_name=t1.comp_name
Order By files.comp_name DESC limit 12