老兄,两个表通过什么关联,你可以使用Left join写的

解决方案 »

  1.   

    code      //代码  主关键字
    date      //交易日期  主关键字
    fOpen
    fHigh
    fLow
    fClose
    fVolume
    dr
    tq
    select table1.code,table1.date,fOpen,fHigh,fLow,fClose,fVolume,dr,tq
    from table1,table2
    where 
    table1.code = table2.code and 
    table1.date >= table2.date and 
    table1.date = 
               (select max(date) from table2 where code = table1.code) 
    order by patientinfo.checkid
      

  2.   

    select table1.code,table1.date,fOpen,fHigh,fLow,fClose,fVolume,dr,tq
    from table1,table2
    where 
    table1.code = table2.code and 
    table1.date >= table2.date and 
    table1.date = 
              (select max(date) from table2 where code = table1.code) 
      

  3.   

    谢谢你,但你这样可能会选不出结果来.table1.date = 
              (select max(date) from table2 where code = table1.code) 
    table1.date存在,但max(date) 极有可能比table1.date小.谢谢你.
      

  4.   

    select b.*,a.dr,a.tq,from (select top 1 '2001-12-01' as date,dr,tq from table2 where date<="2001-12-01' order by date desc) a,table1 b where a.date=b.date
    你试一试吧,我不知道对不对。我现在没法试
      

  5.   

    select table1.code,table1.date,fOpen,fHigh,fLow,fClose,fVolume,dr,tq
    from table1,table2
    where 
    table1.code = table2.code and 
    table1.date >= table2.date and 
    //这回没问题了
    table2.date = 
              (select max(date) from table2 where code = table1.code) 
      

  6.   

    哈哈这个问题有很多方法阿!
    上面的方法太差了!我想功能也不完善!
    他们如何相等那!
    select a.* ,b.dr,b.tq
    from table1 a,table2 b
    where 
    a.code=b.code and decode(a.date-b.date,0,a.date=b.date,max(a.date-b.date))
    我想这个问题的思路是这样的!具体问题你看看
    sql server下可以
    select a.* ,b.dr,b.tq
    from table1 a,table2 b
    where 
    a.code=b.code and (case a.date-b.date when 0 then a.date=b.date  max(a.date-b.date)))
    哈哈我想不错的!
    我想知道你是那里的朋友阿
      

  7.   

    to nightingstar(午.夜.星) :
       (select max(date) from table2 where code = table1.code) 
    这样只能取出一个date ,对于不同的code 取出的date一样,这样肯定不行
      

  8.   

    感谢大家,在大家的讨论下,我已经成功了.
    看看我写的代码.在SQL7.0下运行正常
    SELECT table1.fOpen, table1.fHigh, table1.fLow, 
          table1.fClose, table1.fVolume, table1.fAmount, table2.DR, 
          table2.TQ, table1.date, table2.Date
    FROM table2 INNER JOIN
                table1 ON table2.code = table1.code
    WHERE (table2.Date =
              (SELECT MAX(table2.Date)
             FROM table2
             WHERE (table2.Code = table1.code) AND 
                   (table2.Date <= table1.date)))
      

  9.   

    我写一个,试过可以。
    SELECT b_table1.*, b_table2.f3, b_table2.f4,  b_table2.Date as date2
    FROM b_table2 ,
                b_table1 ,
       ( 
    SELECT MAX(b_table2.Date) as date2 ,b_table1.code,b_table1.Date  FROM b_table2 ,b_table1
            WHERE (b_table2.Code = b_table1.code) AND (b_table2.Date <= b_table1.date) 
     group by b_table1.code , b_table1.Date
       ) t3 
    where   b_table2.code = t3.code and b_table1.code =t3.code and b_table1.date =t3.date and b_table2.date = t3.date2