使用Group By从mySql表中检索行

时间:2012-05-04 10:54:19

标签: asp.net mysql sql

我有一张桌子

Table

我使用以下查询

select max(dblVersion),sName,fCompleted,ixLastModifiedBy,dtCreatedAt,dtLastModifiedAt,fStatus from tblechecklisttemplateversion group by ixTemplate

我将下表作为输出 Table2

其他列的行值与dblVersion列不对应。如何显示相应的行值

2 个答案:

答案 0 :(得分:1)

试试这个

SELECT 
   a.dblVersion,
   a.sName,
   a.fCompleted,
   a.ixLastModifiedBy,
   a.dtCreatedAt,
   a.dtLastModifiedAt,
   a.fStatus 
FROM 
   tblechecklisttemplateversion a
JOIN (
    SELECT 
        ixTemplate,
        max(dblVersion) as dblVersion 
    FROM 
        tblechecklisttemplateversion 
    GROUP BY 
        ixTemplate) as b
ON 
    a.ixTemplate=b.ixTemplate and a.dblVersion=b.dblVersion

答案 1 :(得分:0)

这可能对你有帮助..

MySQL - Get row number on select

更改了查询 -

  SELECT 0 INTO @x;
  select (@x:=@x+1) as  
  rownumber,max(dblVersion),sName,fCompleted,ixLastModifiedBy,dtCreatedAt,
  dtLastModifiedAt,fStatus 
  from tblechecklisttemplateversion group by ixTemplate