有两表:设为a,b
a表:                     b表             
------                  ---------------------
a_id    distance            price
1         0                 10     
2         20                 20     
3         45                50     
4         75                80     
5         31                 100     
6         10                 150     
7         32                 200   
要求结果为:
--------------------------------------
a_id    distance            price
1         0                  10   
2         20                 20     
3         45                 50     
4         75                 80     
5         31                 50     
6         10                 10     
7         32                 50   
即每条记录中,
先取distance值<=b表的price
然后,取b表中最小的值。
  怎么完成?挂月说是:
select a.id,a.distance,min(b.price) from 
a,b where a.distance<b.price
group by a.id,a.distance可是如果a表和b表里还有很多字段时,怎么才能全部都在dbgrid中显示出来。
假设a表有:a_id    distance  A_1 a_2  a_3
    b  表有:  price  b_1 B_2 B_3
我需要的是在前面显示的记录结果后,在每条记录后再加上这些字段值
即记录总数不变,但显示的列数里增加上a表和b表里要用的其它字段,
怎么做?
    
             

解决方案 »

  1.   

    select a.id,a.distance,a.A_1,.a.a_2,b.price,b.b_1,b.b_2 from 
    a join b on a.distance<b.price
    and b.price=(select min(b.price) from 
                  b2 where a.distance<b2.price
                  group by a.id,a.distance)抽象写得~~这个比原题难~~我手头没有环境可以测试,楼主试试我中午再看看
      

  2.   

    select a.a_id,a.distance,(select top 1 price from b where b.price >= a.distance) as price
      from a
      

  3.   

    select a.a_id,a.distance,(select min(price) from b where b.price >= a.distance) as price
      from a