首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 急~~~~~~~请教关于having的使用 [已结贴,结贴人:pbiao]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 17:05:34 楼主
    我有一表,内容如下 

    字段1   字段2 
    1      A 
    1      B 
    2      B 
    2      C 
    1      D 
    2      D 
    1      E 

    现在写一SQL语句,以字段1为主键,只要在1,和2中都出现了的字段2,就显示,最后结果为 
    字段1   字段2 
    1      B 
    2      B 
    1      D 
    2      D 


    我这样写的SQL 
    select 字段1,字段2 
    from tablename 
    group by 字段1,字段2 
    having COUNT(字段2)>2 


    可结果怎么不对那?那位指点一下 
    20  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • liangCK
    • 等级:
    发表于:2008-05-09 17:10:181楼 得分:5
    SQL code
    发帖..发一个就可以了. create table tb(col1 int,col2 nvarchar(5)) insert into tb select 1,'' insert into tb select 1,'' insert into tb select 2,'' insert into tb select 2,'' insert into tb select 1,'' insert into tb select 2,'' insert into tb select 1,'' select * from tb where col2 in(select col2 from tb group by col2 having count(*)>=2) drop table tb /* col1 col2 ----------- ----- 1 B 2 B 1 D 2 D (所影响的行数为 4 行) */
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 17:12:362楼 得分:0
    SQL code
    --> 测试数据: #T if object_id('tempdb.dbo.#T') is not null drop table #T create table #T (C1 int,C2 varchar(1)) insert into #T select 1,'A' union all select 1,'B' union all select 2,'B' union all select 2,'C' union all select 1,'D' union all select 2,'D' union all select 1,'E' select distinct * from #T as t where (select count(distinct c1) from #T where c2=t.c2)=2 order by 2 /* C1 C2 ----------- ---- 1 B 2 B 1 D 2 D */
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 17:18:103楼 得分:5
    SQL code
    declare @t table(字段1 int,字段2 varchar) insert into @t select 1,'A' insert into @t select 1,'B' insert into @t select 2,'B' insert into @t select 3,'C' insert into @t select 1,'D' insert into @t select 2,'D' insert into @t select 1,'E' select * from @t where 字段2 in(select 字段2 from @t group by 字段2 having count(1)>1)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 17:22:464楼 得分:0
    SQL code
    --理解错搂主意思了。。再来 declare @t table(字段1 int,字段2 varchar) insert into @t select 1,'A' insert into @t select 1,'B' insert into @t select 2,'B' insert into @t select 3,'C' insert into @t select 1,'D' insert into @t select 2,'D' insert into @t select 1,'E' insert into @t select 3,'E' select * from @t where 字段2 in (select 字段2 from @t where 字段1 in(1,2) group by 字段2 having count(1)>1)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • cson_cson
    • 等级:
    发表于:2008-05-09 17:27:175楼 得分:0
    这个才是正解吧,如果要唯一,在 * 前加 distinct
    SQL code
    select * from @t a where 字段1 = 1 and exists(select 1 from @t where 字段1 =2 and 字段2=a.字段2)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zzyyc
    • 等级:
    发表于:2008-05-09 17:32:216楼 得分:5
    使用 HAVING 子句选择行
    HAVING 子句对 GROUP BY 子句设置条件的方式与 WHERE 子句和 SELECT 语句交互的方式类似。WHERE 子句搜索条件在进行分组操作之前应用;而 HAVING 搜索条件在进行分组操作之后应用。HAVING 语法与 WHERE 语法类似,但 HAVING 可以包含聚合函数。HAVING 子句可以引用选择列表中出现的任意项。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fuanwei
    • 等级:
    发表于:2008-05-09 17:56:297楼 得分:5
    SQL code
    create table tb(col1 int,col2 nvarchar(5)) insert into tb select 1,'' insert into tb select 1,'' insert into tb select 2,'' insert into tb select 2,'' insert into tb select 1,'' insert into tb select 2,'' insert into tb select 1,'' select * from tb left join (select col2 from tb group by col2 having count(*)>=2) as a on a.col2=tb.col2 where a.col2 is not null drop table tb
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 19:13:328楼 得分:0
    SQL code
    select a.字段1,a.字段2 ( select 字段1,字段2 from tab where 字段1=1 )as a inner join ( select 字段1,字段2 from tab where 字段1=2 )as b on a.字段2 = b.字段2 union select b.字段1,b.字段2 ( select 字段1,字段2 from tab where 字段1=1 )as a inner join ( select 字段1,字段2 from tab where 字段1=2 )as b on a.字段2 = b.字段2
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 19:14:579楼 得分:0
    SQL code
    select * from @t where 字段2 in (select 字段2 from @t group by 字段2 having count(1)>1)
    修改 删除 举报 引用 回复

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