请问一下这个语句如何写?

Lansie 2008-06-12 06:10:19
id class type
1 1 A
2 1 B
3 2 A
4 1 D
5 2 C

现在需要把type中既有'A'又有'B'的class选出来

类似的语句怎么写?

谢谢了
...全文
173 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lansie 2008-06-13
  • 打赏
  • 举报
回复
谢谢大家的回答
我是想看一下有什么好的办法可以解决这一类问题

上面有不少兄弟的答复都可以解决

但如果再增加几个条件呢,比如要有'A','B','C','D','E'的class选出来
这样答复中的解法就有点繁琐了
cxmcxm 2008-06-13
  • 打赏
  • 举报
回复
select distinct class from 表 as a 
where exists(select * from 表 where class=a.class and type='A')
and exists(select * from 表 where class=a.class and type='B')
-晴天 2008-06-13
  • 打赏
  • 举报
回复
declare @tb table(id int,class int,type varchar(50))
insert into @tb select 1,1,'A'
insert into @tb select 2,1,'B'
insert into @tb select 3,2,'A'
insert into @tb select 4,1,'D'
insert into @tb select 5,2,'C'
insert into @tb select 6,1,'C'

select class from @tb where type in('A','B','C','D') group by class having count(distinct type)=4

/*
class
-----------
1

(1 行受影响)
*/
-晴天 2008-06-13
  • 打赏
  • 举报
回复
declare @tb table(id int,class int,type varchar(50))
insert into @tb select 1,1,'A'
insert into @tb select 2,1,'B'
insert into @tb select 3,2,'A'
insert into @tb select 4,1,'D'
insert into @tb select 5,2,'C'

select distinct class from @tb a where exists (select 1 from @tb where class=a.class and type='A') and type='B'

/*
class
-----------
1

(1 行受影响)
*/
changweishao 2008-06-13
  • 打赏
  • 举报
回复

select class,* from tbName where type in('A','B')
win7cc 2008-06-13
  • 打赏
  • 举报
回复
select ....from...where ...or...
tyrone_wang 2008-06-13
  • 打赏
  • 举报
回复
select class
from 表
where type='A'
intersect
select class
from 表
where type='B'

或者
select class
from 表
where type='A' and class in
(
select class
from 表
where type='B'
);
rhq1234 2008-06-13
  • 打赏
  • 举报
回复
select class
from 表
where type in('a','b')
group by class
having count(distinct type) >= 2
灰太狼 2008-06-13
  • 打赏
  • 举报
回复

create table #t1(id int,class int, [type] nvarchar(10))

insert into #t1
select 1, 1 ,'A' union all
select 2, 1 , 'B' union all
select 3 , 2 , 'A' union all
select 4 ,1 , 'D' union all
select 5 ,2, 'C'

select * from #t1

select a.class from #t1 as a
join #t1 as b on a.class = b.class
where a.[type] = 'A' and b.[type] = 'B'

rcshadow 2008-06-13
  • 打赏
  • 举报
回复
我的方法简单
declare @tb table(id int,class int,type varchar(50))
insert into @tb select 1,1,'a'
insert into @tb select 2,1,'b'
insert into @tb select 3,2,'a'
insert into @tb select 4,1,'d'
insert into @tb select 5,2,'c'

select class ,type from @tb
where type='a' or type='b'

如果多就用type in ('a','b','c','d')
hanjs 2008-06-12
  • 打赏
  • 举报
回复

declare @tb table(id int,class int,type varchar(50))
insert into @tb select 1,1,'a'
insert into @tb select 2,1,'b'
insert into @tb select 3,2,'a'
insert into @tb select 4,1,'d'
insert into @tb select 5,2,'c'
select a.class
from @tb a inner join @tb b
on a.class = b.class and b.type='b' and a.type = 'A'

yinle2008 2008-06-12
  • 打赏
  • 举报
回复
-- -- select * from student
-- -- create table data(
-- -- idc char(2),
-- -- class char(2),
-- -- type char(1))
-- -- select * from data
-- -- insert into data
-- -- values ('5','2','c');
-- -- select * from data
-- select * from data
-- where type ='a'or type ='b';
律己修心 2008-06-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 happyflystone 的回复:]
SQL codedeclare @tb table(id int,class int,type varchar(50))
insert into @tb select 1,1,'a'
insert into @tb select 2,1,'b'
insert into @tb select 3,2,'a'
insert into @tb select 4,1,'d'
insert into @tb select 5,2,'c'
select a.class
from @tb a
inner join (select * from @tb where type = 'B') b on a.class = b.class
where a.type = 'A'

/*
class
-----------
1

(所影响的行数…
[/Quote]
Julyseven 2008-06-12
  • 打赏
  • 举报
回复

select class
from 表
group by class
having count(distinct type) >= 2
-狙击手- 2008-06-12
  • 打赏
  • 举报
回复
declare @tb table(id int,class int,type varchar(50))
insert into @tb select 1,1,'a'
insert into @tb select 2,1,'b'
insert into @tb select 3,2,'a'
insert into @tb select 4,1,'d'
insert into @tb select 5,2,'c'
select a.class
from @tb a
inner join (select * from @tb where type = 'B') b on a.class = b.class
where a.type = 'A'

/*
class
-----------
1

(所影响的行数为 1 行)
*/
wzy_love_sly 2008-06-12
  • 打赏
  • 举报
回复
declare @tb table(id int,class int,type varchar(50))
insert into @tb select 1,1,'a'
insert into @tb select 2,1,'b'
insert into @tb select 3,2,'a'
insert into @tb select 4,1,'d'
insert into @tb select 5,2,'c'

select * from @tb t where exists(
select 1 from @tb where class=t.class and type in('a','b')
group by class
having count(distinct type)=2
)


id class type
1 1 a
2 1 b
4 1 d
-狙击手- 2008-06-12
  • 打赏
  • 举报
回复
select a.class
from ta a
left join (select * from ta where type = 'B') b on a.class = b.class
where a.type = 'A'

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧