表1
id date
aa 2009-02-02
aa 2009-02-04
aa 2009-02-09表2
id testdate 测试结果
aa 2009-02-02 1
aa 2009-02-03 2
aa 2009-02-08 3要出来的结果是:
id date 测试结果
aa 2009-02-02 1
aa 2009-02-04 1
aa 2009-02-09 3即是表1需要在表2中取测试结果,date>=testdate,并且最大的testdate那行测试结果。这样的SQL应该怎么写?麻烦各位老大帮帮助。
id date
aa 2009-02-02
aa 2009-02-04
aa 2009-02-09表2
id testdate 测试结果
aa 2009-02-02 1
aa 2009-02-03 2
aa 2009-02-08 3要出来的结果是:
id date 测试结果
aa 2009-02-02 1
aa 2009-02-04 1
aa 2009-02-09 3即是表1需要在表2中取测试结果,date>=testdate,并且最大的testdate那行测试结果。这样的SQL应该怎么写?麻烦各位老大帮帮助。
应该是:
aa 2009-02-02 1
aa 2009-02-04 2
aa 2009-02-09 3
from a,b t
where a.date>=t.testdate
and a.id=t.id
and not exists(select 1 from b
where id=t.id and testdate<=a.date
and testdate>t.testdate)