首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 问个SQL,达人帮!! [已结贴,结贴人:birdwings1982]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 13:20:54 楼主
      现有表A,其中有两个字段a,b,问从表A中抽出数据,要求A.a或A.b有重复,另抽出一张表A.a,A.b无重复且这两项不等于空白.
    50  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 13:23:211楼 得分:0
    SQL code
    --不会是这样吧? selectt * from A where a=b selectt * from A where a!=b and ianull(a,'')!='' and ianull(b,'')!=''
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 13:23:212楼 得分:0
    我是楼猪补充一下,就是说第一次从表中抽出所有重复的数据,第二次抽出无重复且无空的数据
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 13:24:543楼 得分:0
    1楼没理解。。我的意思是A.a!=A.a;B.b!=B.b。。也就是本来有重复的数据,把重复的那几条抽出来形成一个表。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 13:25:544楼 得分:0
    select *
    from ta a
    where exists(select 1 from ta where a.a = a or a.b= b)


    select *
    from ta a
    where not exists(select 1 from ta where a.a= a and a.b = b)
          and a is not null and b is not null
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 13:26:145楼 得分:0
    再补充下,a,b都不是Ukey所以数据库里那两项一样的有很多重复数据,现在要抽出重复数据。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 13:26:286楼 得分:30
    问从表A中抽出数据,要求A.a或A.b有重复
    SQL code
    select * from a x where exists ( select 1 from a where id<>a.id and (a=x.a or b=a.b) )


    另抽出一张表A.a,A.b无重复且这两项不等于空白.
    SQL code
    select * from a x where a<>'' and b<>'' and not exists ( select 1 from a where id<>a.id and a=x.a ) and not exists ( select 1 from a where id<>a.id and b=x.b )
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 13:28:037楼 得分:0
    假设id是主键,必须要有主键或者唯一索引
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 13:29:358楼 得分:0
    谢谢大家的热情,我试一下,没问题立马给分。。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 13:31:489楼 得分:0
    select distinct a,b from A where isnull(a+b,'')>''
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 13:32:2710楼 得分:20
    引用 4 楼 happyflystone 的回复:
    select *
    from ta a
    where exists(select 1 from ta where a.a = a or a.b= b)


    select *
    from ta a
    where not exists(select 1 from ta where a.a= a and a.b = b)
          and a is not null and b is not null


    我的错了,是得有ID主键 

    select *
    from ta a
    where exists(select 1 from ta where id <> a.id and ( a.a = a or a.b= b))


    select *
    from ta a
    where not exists(select 1 from ta where a.id <> id and a.a= a and a.b = b)
          and a is not null and b is not null
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 13:54:4311楼 得分:0
    SQL code
    select * from a where a in (select a from a group by a having count(a)>1) or b in (select b from a group by b having count(b)>1) select * from a where (a in (select a from a group by a having count(a)=1) or b in (select b from a group by b having count(b)=1)) and a<>'' and b<>''
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • ojuju10
    • 等级:
    发表于:2008-05-12 13:59:5112楼 得分:0
    SQL code
    现有表A,其中有两个字段a,b,问从表A中抽出数据,要求A.a或A.b有重复,另抽出一张表A.a,A.b无重复且这两项不等于空白. select a,b from tba where a<>'' and b<>'' group by a,b
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • ojuju10
    • 等级:
    发表于:2008-05-12 14:00:5313楼 得分:0

    SQL code
    select a,b from tba where (a<>'' or a is not null) and (b<>'' and b is not null) group by a,b
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • cson_cson
    • 等级:
    发表于:2008-05-12 14:08:2414楼 得分:0
    SQL code
    select * from ta a where (select count(*) from a.a = a and a.b = b ) = 1
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    世纪乐知(北京)网络技术有限公司 版权所有 京 ICP 证 020026 号
    Copyright © 2000-2007, CSDN.NET, All Rights Reserved