关于对同一张表的同一字段进行比较(如内外表查询):
如:select a.a1 from A a
where(select a1 from A where a1>a.a1)等等.
像这样的对同一张表的相同字段比较到底是出什么的结果!

解决方案 »

  1.   

    select a.a1 from A a
    where(select a1 from A where a1>a.a1) 语法错误
    select a.a1 from A a
    where not exists(select a1 from A where a1>a.a1)
    得到最大值
    select max(a1) from A
      

  2.   

    create table #a(a int,b int) 
    insert into #a select 1,16 
    insert into #a select 2,15 
    insert into #a select 3,14 
    insert into #a select 4,13 
    insert into #a select 5,12 
    insert into #a select 6,11 
    select a.a from #a a
    where not exists(select a from #a where a>a.a)
    a           
    ----------- 
    6(所影响的行数为 1 行)
      

  3.   

    select max(a1) from A, 最大值
      

  4.   

    select a.a1 from A a
    where not exists(select a1 from A where a1>a.a1)
    得到最大值
    select a.a1 from A a
    where exists(select a1 from A where a1>a.a1)
    得到除最大值以外的值