select distinct * from (MainInfo as m inner join otherInfo as o on m.ContractNo=o.ContractNo and m.RegDate=o.RegDate and m.Refundtimes=o.Refundtimes)
inner join (select max(RegDate) as mregdate,ContractNo,Refundtimes from otherinfo group by ContractNo,Refundtimes ) as n on m.ContractNo=n.ContractNo and m.RegDate=n.mRegDate and m.Refundtimes=n.Refundtimes 
 where m.CardId='1111'
我这个是按你的条件写出来了。不能与你的要的结果相同,就是因为你的条件有问题。
其中有两条记录是同RegDate和CordID,不同refundtimes。你的条件没有针对这类问题进行处理。
你也提出:“碰到同一RegDate不同的refundtimes就不对了”。
另外,我觉得你的数据库结构不科学,两个表都有CardID字段,但内容不相同。还有就是两个表重复字段太多(三个),一般只要一个就够了。

解决方案 »

  1.   

    select a.*  from  otherInfo  a   inner join
    (select  ContractNo, RegDate, Refundtimes=max(Refundtimes)  from otherInfo
    Group by  ContractNo, RegDate,CardId )  b 
     on(a.ContractNo=b.ContractNo and a.RegDate=b.RegDate  and  a.Refundtimes=b.Refundtimes)
    inner join maininfo  c 
     on(a.ContractNo=c.ContractNo and a.RegDate=c.RegDate  and  a.Refundtimes=c.Refundtimes)
    where  c.cardid=1111
      

  2.   

    --------上面那句錯了,少了一個條件,下面是正確的
    Create  Table  MainInfo(ContractNo  varchar(5), RegDate  datetime,Refundtimes  int,CardId   int)
    insert into MainInfo
    select '00001',  '2004-01-01'  ,  1, 1111  union  all
    select '00001' , '2004-01-01'  ,  2 , 1111 union  all
    select '00002'  ,'2005-02-01' ,   1  ,  1111 union  all
    select '00003'  ,'2005-02-02',    1  , 2222
    -------------the  second  table ------------------------------
    create  table  otherInfo(ContractNo  varchar(5), RegDate  datetime, Refundtimes  int, CardId  varchar(20),Name   varchar(12),   ManageAddress  varchar(20))
    insert into  otherInfo
    select  '00001', '2004-01-01'  ,  1,  '332623800411477' , 'yhd0411',  '浙江'  union  all
    select '00001' ,  '2004-01-01',    1,'332623550129611' , '张三' ,    '浙江' union  all
    select '00001' ,  '2004-01-01',    1,'332603670318156' , '李四' ,    '北京' union  all
    select '00001'  , '2004-01-01',    2,'332623800411477' , 'yhd0411',  '浙江' union  all
    select '00001'   ,'2004-01-01',    2,'332623550129611',  '张三','浙江'
    -------------------------下面實現  -------------------------------
    select a.*  from  otherInfo  a   inner join
    (select  ContractNo, RegDate,cardid, Refundtimes=max(Refundtimes)  from otherInfo
    Group by  ContractNo, RegDate,CardId )  b 
     on(a.ContractNo=b.ContractNo and a.RegDate=b.RegDate  and  a.Refundtimes=b.Refundtimes  and a.cardid=b.cardid)
    inner join maininfo  c 
     on(a.ContractNo=c.ContractNo and a.RegDate=c.RegDate  and  a.Refundtimes=c.Refundtimes)
    where  c.cardid=1111
      

  3.   

    簡化後的
    select a.cardid,a.name,a.ManageAddress,a.ContractNo,a.RegDate,a.Refundtimes  from  otherInfo  a   inner join
    (select  ContractNo, RegDate,cardid, Refundtimes=max(Refundtimes)  from otherInfo
    Group by  ContractNo, RegDate,CardId )  b 
     on(a.ContractNo=b.ContractNo and a.RegDate=b.RegDate  and  a.Refundtimes=b.Refundtimes  and a.cardid=b.cardid)
    inner join maininfo  c 
     on(a.ContractNo=c.ContractNo and a.RegDate=c.RegDate  and  a.Refundtimes=c.Refundtimes)
    where  c.cardid=1111
      

  4.   

    To:QQ1368065(桃花依旧) 表结构是人家留下的,唯一索引就是Contractno+RegDate+Refundtimes表MainInfo中的CardId 是客户的证件号码表OtherInfo中的CardId 是关联客户的证件号码那表结构应当怎样设计???
      

  5.   

    To:Softlee81307(孔腎) 
    这样查找第一次好像很慢是不是要建立存储过程好点???
      

  6.   

    otherInfo ,maininfo  要建立索引 就不會 慢了