有一个从数据库表中检索数据的小程序,请大家给一点思路,这个表中数据的特点是:(1,aa,32,23,2005-2-23);(2,aa,33,55,2005-3-24);(3,bb,24,45,2005-3-25);(4,bb,56,58,2005-4-30)等等,特点是有多个记录的某个字段相同(如:aa),其它字段不相同,检索条件是在这些有相同字段的记录中,想只检索出最后被写数据库表中的那条记录,另外只想检索出这个表中的部分字段,而不是所有的字段。
请大家给一点思路吧,我用的是DataGrid控件,结果只能检索出表中表有的数据,很郁闷!

解决方案 »

  1.   

    select top 1 字段1,字段2,字段n from 表名 order by id(根据你那个数据表的‘特点’应是第一个字段)
      

  2.   

    select top 1 字段1,字段2,字段n from 表名 order by id(根据你那个数据表的‘特点’应是第一个字段) desc
      

  3.   

    select top 1 字段1,字段2,字段n from 表名 where 字段='你要求的字段值' order by 日期字段 desc
      

  4.   

    create table t(id int identity(1,1),col1 char(2),int1 int,int2 int,date1 datetime)
    insert t select 'aa',32,23,'2005-2-23'
    union all select 'aa',33,55,'2005-3-24'
    union all select 'bb',24,45,'2005-3-25'
    union all select 'bb',56,58,'2005-4-30'select col1,convert(varchar(10),max(date1),120) [date]  from t  group by col1
    /*
    result:
    col1 date       
    ---- ---------- 
    aa   2005-03-24
    bb   2005-04-30
    */
    drop table t
      

  5.   

    请问一下我用下面的SQL语句可以吗?我觉得是可以的,但是一直有语法错误:
    select top 1 AutoNum,AccountID,Income,CreateDate from AccountLog group by AccountID order by CreateDate desc;错误提示为:试图执行的查询中不包含作为合计函数一部分的特定表达式‘AutoNum'。
    好像是group by有语法错误,但是不知道错在什么地方,我试过即使用下面的SQL语句
    select  AutoNum,AccountID,Income,CreateDate from AccountLog group by AccountID ;
    也会出现同样的提示!请问一下应该怎样解决?
      

  6.   

    是不是要每个AccountID的最后一个CreateDate的那行记录?select AutoNum,AccountID,Income,CreateDate 
    from AccountLog a
    where CreateDate=(
    select max(CreateDate) 
    from AccountLog
    where AccountID=a.AccountID
    )
      

  7.   

    sql server语句,其他数据库可能不适合!!
      

  8.   

    这个SQL语句在access中也可以实现,真是的太感谢Haiwer(海阔天空) !!!