TengChenLi 2012-12-13 23:08
浏览 646
已采纳

oracle 多字段重复记录查询

有时候我们会遇到这种情况,数据表中有两个或者多个字段相同的记录,但我们只想挑选出这些相同记录中的一条,如下:

 

id name age sex address
1 张三 20 南京
2 张三 20 上海
3 王五 23 无锡
4 李斯 25 北京

我想查询时剔除姓名、年龄和性别相同的记录,查询结果想如下形式:

 

 

id name age sex addrs
1 张三 20 南京
2 王五 23 无锡
3 李斯 25 北京

请问老师们 有啥好的方法支招。之前在网上查到通过rowid来查询,如下:

select t1.* from table t1 where rowid in   (select min(rowid)  from  table t2  where  t1. name=t2.name and t1. age=t2.age and t1.sex=t2.sex)   但是用改语句有点问题,就是有时候可以查到想要的值,有时候查询记录为空,不止为何?请知道的朋友多多指点

 

 

  • 写回答

2条回答 默认 最新

  • jinnianshilongnian 2012-12-14 00:08
    关注

    select t1.*
    from tbl t1
    where rowid in (
    select min(rowid) from tbl t2 where t1. name=t2.name and t1. age=t2.age and t1.sex=t2.sex
    )

    sql没问题的。不知道你是怎么查的( 用的工具吗? 我用的pl/sql developer 8.0有时候抽风),什么情况下是空?

    exists写法
    select t1.*
    from tbl t1
    where exists (
    select * from tbl t2 where t1. name=t2.name and t1. age=t2.age and t1.sex=t2.sex group by t2.name,t2.age,t2.sex having t1.rowid=min(t2.rowid)
    )

    如果不是删除,只是查询的话,可以通过分组搞定:
    select min(id),min(address), name, sex, age
    from tbl t1
    group by name, sex, age;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名