有一表a有字段phone,code,表b,有字段,phone,code,content,time,
现在需要表a 每个phone最后时间的sql语句:phone,code,content,time
time就是离现在最近的时间,help

解决方案 »

  1.   

    select * from 表 a where not exists(select * from 表 where phone=a.phone,code=a.code,content=a.content,time>a.time)
      

  2.   

    a表和b表什么关系?为什么不直接从b表里取?
      

  3.   

    select * from 表 a where not exists(select * from 表 where phone=a.phone,time>a.time)
      

  4.   

    晕,低级错误~把上面的逗号改成and
    select * from 表 a where not exists(select * from 表 where phone=a.phone and time>a.time)
      

  5.   

    没说清楚,不好意思
    A表
    phone code
    111    aa
    111    bb
    222    bb
    B表
    phone code content time
    111    aa   fasdf   2006-04-10
    111    aa   sdfss   2006-04-11
    222    bb    ddda   2006-04-12
    111    bb   fdsfs   2006-04-13
    根据表a,
    111 aa  sdfss    2006-04-11
    111  bb  fdsfs   2006-04-13
    222  bb  ddda    2006-04-12
    sql怎么写啊
      

  6.   

    declare @t table(phone varchar(6), code varchar(6))
    declare @t1 table(phone varchar(6), code varchar(6) , content varchar(10), time datetime)
    insert into @t
    select '111', 'aa' union 
    select '111', 'bb' union 
    select '222', 'bb' insert into @t1
    select '111', 'aa', 'fasdf', '2006-04-10' union 
    select '111', 'aa', 'sdfss', '2006-04-11' union 
    select '222', 'bb', 'ddda', '2006-04-12' union 
    select '111', 'bb', 'fdsfs', '2006-04-13'select *
    from @t1 a
    where a.time in
    (
      select max(time) from @t1 b
      where b.phone = a.phone and b.code = a.code 
    )不知道可以满足楼主的要求吗
      

  7.   

    谢谢,回复,
    不行啊 ,好像@t都没用到阿
    zjdyzwx(十一月猪)