标题:求一句SQL语句。(急)
只看楼主
power6d
Rank: 1
等 级:新手上路
帖 子:162
专家分:0
注 册:2006-5-23
 问题点数:0 回复次数:2 
求一句SQL语句。(急)
表中的数据如下:

字段1 字段2
AAA 111
AAA 222
AAA 333
BBB 111
BBB 222
CCC 111
CCC 222
DDD 111
DDD 222
DDD 333

我要找出
每个字母组中没有333的记录

查出的结果是:
字段1
BBB
CCC

请各位高手帮帮忙,很急!
搜索更多相关主题的帖子: SQL 语句 
2007-03-12 10:48
棉花糖ONE
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:2987
专家分:0
注 册:2006-7-13
得分:0 

create table shiyan(col1 char(3),col2 int)
insert into shiyan select 'AAA',111
union select 'AAA',222
union select 'AAA',333
union select 'BBB',111
union select 'BBB',222
union select 'CCC',111
union select 'CCC',222
union select 'DDD',111
union select 'DDD',222
union select 'DDD',333

select distinct col1 from shiyan s1 where not exists(select 1 from shiyan s2 where s1.col1=s2.col1 and s2.col2=333)

select distinct col1 from shiyan s1 where col1 not in (select col1 from shiyan where col2=333)


26403021 sql群 博客 blog./user15/81152/index.shtml
2007-03-12 11:03
power6d
Rank: 1
等 级:新手上路
帖 子:162
专家分:0
注 册:2006-5-23
得分:0 
已经解决了。呵呵,脑子一下子没转过来。

以下是答案:
Declare @Tmp1 Table
(
Field1 Char(5),
Field2 Char(5)
)

Insert Into @Tmp1
Select 'AAA','111' Union
Select 'AAA','222' Union
Select 'AAA','333' Union
Select 'BBB','111' Union
Select 'BBB','222' Union
Select 'CCC','111' Union
Select 'CCC','222' Union
Select 'DDD','111' Union
Select 'DDD','222' Union
Select 'DDD','333'

Select Distinct Field1 From @Tmp1 A Where Not Exists(Select * From @Tmp1 B Where Field2='333' And A.Field1=B.Field1)

2007-03-12 11:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-123770-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.057343 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved