select table2.d,table2.e from table1,table2 where table2.d >= table1.a and table2.d <=table1.b and table1.e <= table2.c 

解决方案 »

  1.   

    if object_id('[table1]') is not null drop table [table1] 
     go 
    create table [table1]([a] int,[b] int,[c] int)
    insert [table1] select 0,100,10
    union all select 100,200,20if object_id('[table2]') is not null drop table [table2] 
     go 
    create table [table2]([d] int,[e] int)
    insert [table2] select 90,12
    union all select 120,13select * from table2 t where exists(select 1 from table1 where t.d between a and b and t.e<=c)
    /*
    d           e
    ----------- -----------
    120         13(1 行受影响)
    */
      

  2.   

    楼主最后一个条件 且 table1.e <= table2.c  写错了,应该是:
    table2.e <= table1.c  吧?create table table1(a int,b int,c int)
    insert table1 values(0,100,10)
    insert table1 values(100,200,20)create table table2(d int,e int)
    insert table2 values(90,12)
    insert table2 values(120,13)select table2.d,table2.e from table1,table2 where table2.d >= table1.a and table2.d <=table1.b and table2.e <= table1.c 
    /*
    d           e
    ----------- -----------
    120         13
    */
      

  3.   

    declare @tabl1 table(a int,  b int,  c int)
     
    insert @tabl1 select 0 , 100 , 10 
    insert @tabl1 select 100, 200, 20 declare @table2 table(d int,e int)
    insert @table2 select 90,  12 
    insert @table2 select 120 ,13 
    select * from @table2 a where exists(select 1 from @tabl1 where a.d >= a and a.d <=b and a.e<=c )
    /*
    d           e           
    ----------- ----------- 
    120         13(所影响的行数为 1 行)
    */
      

  4.   

    呵呵.你把条件都说完了.SQL是面向集合的查询.我们需要关注获取什么数据,而不是怎么获取数据(这个让SQL操心吧.)当然,也存在少部分需求使用游标处理的效率比基于集合的解决方案高.