表A:
peono    peoname  cardno    opertype         handletime
00296584 张三 100328 申请用户卡 2007-08-22 15:37:35.607
00296584 张三 000566 换卡         2007-08-23 09:05:29.187
00083144 李四 052314 申请用户卡 2007-08-22 15:27:20.000
00083144 李四 052314 换卡         2007-08-23 10:11:26.530
00296013 王五 021108 申请用户卡 2007-08-22 15:50:10.547
00296013 王五 021108 换卡         2007-08-23 10:56:23.390
00053973 张飞 119922 申请用户卡 2007-08-23 08:52:07.340
00053973 张飞 105603 换卡         2007-08-23 09:04:20.263
......如何买语句查询如下结果,申请用户卡与换卡卡号不一致的peono:-------------
peono00296584
00053973

解决方案 »

  1.   

    select 
        distinct m.peono,n.peoname 
    from 
        表A m,表A n 
    where 
        m.peono=n.peono 
        and 
        m.opertype='申请用户卡' 
        and 
        n.opertype='换卡' 
        and 
        m.cardno<>n.cardno
      

  2.   

    select 
        distinct m.peono
    from 
        表A m,表A n 
    where 
        m.peono=n.peono 
        and 
        m.opertype='申请用户卡' 
        and 
        n.opertype='换卡' 
        and 
        m.cardno<>n.cardno
      

  3.   

    select distinct peono from 表名 a where exists(select 1 from 表名 where a.peono=peono  and cast(a.cardno as int)-cast(cardno as int)=0)
      

  4.   

    select distinct peono from
    (
      select peono,cardno from a group by peono,cardno having count(*) = 1
    ) t
      

  5.   

    select distinct peono from
    (
      select peono,cardno ,count(*) from a group by peono,cardno having count(*) >1
    ) t
      

  6.   

    dawugui(潇洒老乌龟) 兄有筆誤吧
      

  7.   

    前面我写的是错的 对不起
    更正一下
    -------------------
    select distinct peono from 表名 a where  exists
    (select 1 from 表名 where a.peono=peono  and cast(a.cardno as int)>cast(cardno as int)and cast(a.cardno as int)<cast(cardno as int))
      

  8.   

    sorry 又错了对不起
    再次更正一下 这次一定对
    -------------------
    select distinct peono from 表名 a where  exists
    (select 1 from 表名 where a.peono=peono  and (cast(a.cardno as int)>cast(cardno as int) or cast(a.cardno as int)<cast(cardno as int)))
      

  9.   

    select distinct peono from
    (
      select peono,cardno ,count(*) from a group by peono,cardno having count(*) >1
    ) t
      

  10.   

    select distinct peono=(select peono from 表 where peono=a.peono and cardno<>a.cardno) from 表 a
      

  11.   

    dawugui(潇洒老乌龟) 兄有筆誤吧
    ---------------------------
    鹤 兄
    是你自己的写错了吧???
    再看看题目要求~~~~
      

  12.   

    dawugui(潇洒老乌龟) 
    的是正确的!
      

  13.   

    ---试试这种方法,LZ看那种方法效率最快,自行采用吧
    Select peono From 表A As T Where opertype='申请用户卡' And Exists
            (Select 1 From 表A Where peono=T.peono And peoname=T.peoname And cardno<>T.cardno)