Table1有两列:
id     f
1      31
2      40
...
========================
Table2有四列:
id     d1     d2     d
1      23     30     5
2      10     50     6
3      30     35     5
...编写一个sql语句,查到的数据满足下述条件:
f >= d1 and f <= d2 and d=5 table2中d=5的记录
有不确定的多个。即:(f >= 23 and f <= 30) or (f >= 30 and f <= 35) or ...

解决方案 »

  1.   


    select * from table1 a,table2 b where a.id=b.id and b.d=5 and a.f <=b.d2 and a.f >=b.d1 
      

  2.   

    select distinct id, f from (
    select t1.id, t1.f
    from table1 t1 full join table2 t2 on t1.f>=t2.d1 and t1.f<=t2.d2
    where t2.d=5 ) t;
      

  3.   


    --不是很明白需求,不知道是不是这样的?
    --这也太简单了嘛
    select * from tab1 a,tab2 b
    where a.id=b.id and (f between d1 and d2) and d=5
      

  4.   

    create table table1(id number(18,0), f number(18,0));
    create table table2(id number(18,0), d1 number(18,0), d2 number(18,0), d number(18,0));insert into table1(id,f) values(1,31);
    insert into table1(id,f) values(2,40);insert into table2(id,d1,d2,d) values(1,23,30,5);
    insert into table2(id,d1,d2,d) values(2,10,50,6);
    insert into table2(id,d1,d2,d) values(3,30,35,5);commit;select distinct id, f from (
    select t1.id, t1.f
    from table1 t1 full join table2 t2 on t1.f>=t2.d1 and t1.f<=t2.d2
    where t2.d=5 ) t;
      

  5.   

    得到Table1表中的数据?》还是大致如下:左连接,然后判断select * 
    from (
    select t1.id,t1.f ,t2.id,t2.d1,t2.d2,t2.d
    from Table1 t1 left join Table2 t2
    )
    where f>=d1 and f<=d2 and d=5
      

  6.   

    不好意思。是想取table1中的数据。因为我这儿不能用join所以求助了1、3楼多加了个条件a.id=b.id,其他的答案都是join的。只好把1、3的改改用了。
      

  7.   

    顺便问各位大侠一个:我们用的是hibernate,但是设计者却不加表之间的关联,所有写hql时用了join就无效,是这样的吗?