表中结构如下id       nameA1       aaaA2       bbbA3       cccA4       dddA5       eee
我现在想查出来最大的id 应该怎么写? 

解决方案 »

  1.   

    最大ID对应的整行:
    select top 1 * from tb order by id desc
      

  2.   


    --楼上的是按字符串来排序的,--> 测试数据: [tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb] (id varchar(20),name varchar(3))
    insert into [tb]
    select 'A1','aaa' union all
    select 'A2','bbb' union all
    select 'A3','ccc' union all
    select 'A4','ddd' union all
    select 'A5','eee' union all
    select 'A11','eee'select top 1 id from [tb] order by cast(substring(id,patindex('%[0-9]%',id),len(id)) as int) desc--结果:
    A11
      

  3.   

    SELECT TOP 1 *
    FROM dbo.Student
    ORDER BY SName DESC
      

  4.   

    用max,按照字符串排序查找最大时,楼上的是A5,而不是A11
      

  5.   

    select  'A1' id,'aaa' name into #tb 
    union all select  'A2','bbb'
    union all select  'A3','ccc'
    union all select  'A4','ddd'
    union all select  'A5','eee'select MAX(id) max_id from #tb
    /*
    max_id
    ------
    A5(1 row(s) affected)
    */
      

  6.   


    8 a1 aaa 0 男 班长 aa aaa 啊啊啊啊啊
    9 a2 aaa 0 男 班长 aa aaa 啊啊啊啊啊
    10 a3 aaa 0 男 班长 aa aaa 啊啊啊啊啊
    11 b1 NULL NULL NULL NULL NULL NULL NULL
    12 b2 NULL NULL NULL NULL NULL NULL NULL
    13 b3 NULL NULL NULL NULL NULL NULL NULLSELECT TOP 1 *
    FROM dbo.Student
    ORDER BY SName DESC
    13 b3 NULL NULL NULL NULL NULL NULL NULL
      

  7.   

    字符串也是可以比较出大小的 按排序规则  只要你的排序规则是你想要的那种 select MAX(id) max_id from #tb就可以   
      

  8.   

    SELECT ServerProperty('Collation')    --查看当前的排序规则
      

  9.   

    那这个语句的 len(id) as int 是什么意思呢?
    (id,patindex('%[0-9]%',id),len(id)) as int
      

  10.   

    patindex('%[0-9]%',id)是得出id字段中,数字所在的位置,
    然后使用substring进行截取,相当于在id字段中,截取字段里面对应的数字进行排序。。