有一个表,叫table1吧,有比如总序号,子序号这两个字段以及其他的几个字段,
现在的目的就是:先查询出table1的最大的总序号,然后这个查询出的总序号所对应的子序号,然后就查询出来。比方,table1中记录一的有23、45、67这三个总序号,那么我先查找出记录一的67这个总序号,然后67这个总序号又对应了1、2、3、4这四个子序号,那么就还要进一步查出这个4对应的子序号对应的记录来。应该很清楚了吧?最后的查询出的结果应该是:(最终记录一,只会查询出来一条记录)姓名         总序号          子序号             属性
张三           67              4                 红

解决方案 »

  1.   

    select
        t.姓名,
        t.总序号,
        t.子序号,
        t.属性
    from
        table1 t
    where
        not exists(select 1 from table1 where 总序号=t.总序号 and 子序号>t.子序号)
      

  2.   

    select t.姓名,t.总序号,t.子序号 from table1 t where t.姓名='张三' and t.总序号 = (Select MAX(总序号) From ORDERS where 姓名=t.姓名)上面这句话得到最大总序号对应的纪录,但是怎么得到最大子序号对应的呢???拜托了!!   姓名       总序号     子序号
    ---------- ---------- ------------
      张三          14            1
      张三          14            2
      张三          14            3
      张三          14            4
      

  3.   

    select
        t.姓名,
        t.总序号,
        t.子序号,
        t.属性
    from
        table1 t
    where
        not exists(select 1 from table1 where 总序号>t.总序号)
        and
        not exists(select 1 from table1 where 总序号=t.总序号 and 子序号>t.子序号)
      

  4.   

    select
        t.姓名,
        t.总序号,
        t.子序号,
        t.属性
    from
        table1 t
    where
        t.总序号 = (select max(总序号) from table1)
        and
        not exists(select 1 from table1 where 总序号=t.总序号 and 子序号>t.子序号)
      

  5.   

    libin_ftsafe(子陌红尘:当libin告别ftsafe),好快的刀
      

  6.   

    declare @t table(姓名 varchar(100), 总序号 int, 子序号 int, 属性 varchar(100))
    insert @t
    select  '张三', 12, 1,'红' union all
    select  '张三', 12, 2,'红' union all
    select  '张三', 14, 1,'红' union all
    select  '张三', 14, 2,'红' union all
    select  '张三', 13, 1,'红' union all
    select  '张三', 13, 2,'红'select
        t.姓名,
        t.总序号,
        t.子序号,
        t.属性
    from
        @t t
    where
        t.总序号 = (select max(总序号) from @t)
        and
        t.子序号 = (select max(子序号) from @t)/*
    姓名 总序号 子序号 属性
    ----------------------------------
    张三 14 2 红
    */