create table tab1
(
id int primary key identity(1,1),
names varchar(20)
)create table tab2
(
id int primary key identity(1,1),
cl varchar(20),
names varchar(20),
times datetime
)insert into tab1 values('李红')
insert into tab1 values('王五')
insert into tab1 values('张三')insert into tab2 values('铁','李红','2008-12-01')
insert into tab2 values('煤','王五','2008-11-11')
insert into tab2 values('油','李红','2008-10-12')
insert into tab2 values('盐','张三','2008-3-5')
查出 结果如下
李红   2008-12-01
王五   008-11-11
张三   2008-3-5
比如说李红有两条记录,那么查最近时间的一跳记录

解决方案 »

  1.   

    *****  order  by  datetime  desc
    ????
      

  2.   

    tb1贴出来干吗呢?
    如下:
    select names,max(times) from tab2 group by names
      

  3.   

    聚合分组,  LZ这个表设计有问题.! tab2应该引用tab1表的id,把names换成外键id
    select names,max(times) from tab2 group by names
      

  4.   

    如果我想names是tab1那个字段值呢,该怎么些
      

  5.   


    select names,max(times) from tab2 where names in (select names from tbl1) group by names