select * from table2 t
where exists(select 1 from table1 where lie1=t.lie1)
      and
      not exists(select 1 from table2 where lie1=t.lie1 and lie2>t.lie2)

解决方案 »

  1.   

    create table table1
    (
      lie1 varchar(10),
      lie2 varchar(10),
      lie3 varchar(10)
    )
    create table table2
    (
      lie1 varchar(10),
      lie2 int,
      lie3 int
    )
    insert table1
    select 'a','f','g' union
    select 'd','r','m' union
    select 'a','f','f' union
    select 'f','f','f' union
    select 'f','y','i' 
    insert table2
    select 'a',1,5 union
    select 'b',5,6 union
    select 'n',6,7 union
    select 'r',6,8 union
    select 'a',5,6 --测试
    select * from table2 t
    where exists(select 1 from table1 where lie1=t.lie1)
          and
          not exists(select 1 from table2 where lie1=t.lie1 and lie2>t.lie2)
    --删除测试环境
    drop table table1,table2--结果
    /*
    lie1       lie2        lie3        
    ---------- ----------- ----------- 
    a          5           6(所影响的行数为 1 行)
    */
      

  2.   

    select distinct * from  table2  where lie1 in(select lie1 from table1)
      

  3.   

    我有点疑问:
    请问如何取table2中lie1列的数据在table2的lie1中也存在的,并且,要取出的table2的lie1中的数据不重复--请问如何取table2中lie1列的数据在table2的lie1中也存在的--估计笔误,应该是取table2中lie1列的数据在table1的lie1中也存在的--并且要取出的table2的lie1中的数据不重复(你的测试数据table2中lie1列的数据在table1的lie1中也存在的只有lie1=a的2笔,可是如果只说重复,到底哪个重复呢?如果指lie1不重复,那就是没有这样的纪录,如果说整笔不重复,那么就是有2笔罗)