有一个表table如下:
 id
123A
123B
124A
125B
125C
我想查看具有相同前三位的id号查询结果如下所示:
id 
123A
123B
125B
125C

解决方案 »

  1.   

    select a.*
    from tablename a,
    (select left(id,3) as id
    from tablename
    group by left(id,3)
    having count(*)>1) b
    where left(a.id,3)=b.id
      

  2.   

    非常感谢,现在我想,将出现的多条记录删除,只保留一条,并将其id更改为前三位
    结果如下所示:
    id
    123
    125
      

  3.   

    select *
    from tb a
    where exists(select left(id,3) from tb where left(id,3)=left(a.id,3) 
    group by left(id,3) having count(*)>1)
      

  4.   

    select distinct left(a.id,3) 
    from tablename a,
    (select left(id,3) as id
    from tablename
    group by left(id,3)
    having count(*)>1) b
    where left(a.id,3)=b.id