现有a表
dm        mc
001       a
002       ab
003       abcdef
004       abc
005       abcde
N条数据
- - -
如何用SQL语句查找 mc长度最大的数据行

解决方案 »

  1.   

    select top 1 *
    from a
    order by len(mc) desc
      

  2.   

    select top 1 with ties *
    from a
    order by len(mc)desc
      

  3.   


    select * from 
    (select dm,mc,len(mc) as len_mc a ) as A
    where A.len_mc=(select max(len(mc)) from a)
      

  4.   

    --生成测试数据
    if object_id('tb') is not null
    drop table tb
    go
    create tb(dm nvarchar(10), mc nvarchar(50))
    insert tb
    select '001', 'a' union all
    select '002', 'ab' union all
    select '003', 'abcdef' union all
    select '004', 'abc' union all
    select '005', 'abcde'
    --你要的语句
    select top 1 * from tb order by len(mc) desc