有如下两表table1
    No     A     B    C                  
    201    3     x    x
    202    2     y    y
    203    3     z    n
    204    1     m    x
    ..............table2
   No      LDate      D 
   201     02-01-01   m
   201     02-03-05   x
   202     02-05-01   k
   202     02-06-05   l
   203     02-02-01   y
   203     02-06-03   k
现在想得到如下描述的结果
  1,选择table1中所有 A >=2 的记录,
  2,根据1所查询的结果中的No,从table2中选择各个No中LDate最大的那一条记录
  3,合并1,2 按No.够清楚吧!

解决方案 »

  1.   

    select * from (select * from table1 where a>=2)as a
      (select table2.* from table2,(select distinct no,ldate from table2 order by ldate)as c where table2.no=c.no and table2.ldate=c.ldate)as b
    where a.no=b.no
    try it!
      

  2.   

    select tA.* 
      from table2 tA,
           table1 tB
     where tA.No = tB.No
       and tB.A >= 2
       and tA.lDate = (select Max(LDate) from table2 where No = tA.No)
      

  3.   

    select * from (select * from table1 where a>=2)as a
      (select table2.* from table2,(select distinct no,ldate from table2 order by ldate)as c where table2.no=c.no and table2.ldate=c.ldate)as b
    where a.no=b.no
      

  4.   

    select max(table2.LDate) from (select * from table1 where a>=2) Ta ,table2
    where table2.no=ta.no
      

  5.   

    select a.* ,b.* from table2 b,(select * from table1 where a>=2) a
    where b.no=a.no 
    and   b.ldate=select(max(ldate) from table2 where no=a.no)
      

  6.   

    select max(ldate) from table2  a where exists (select no form table b where b.a>2 and no=a.no)