name course
a 语文
a 数学
a 化学
b 语文
b 英语
b 数据库
c 语文
c 数据库
c 物理求既选了数据库又选了物理的人结果
name course
c 语文
c 数据库
c 物理
a 语文
a 数学
a 化学
b 语文
b 英语
b 数据库
c 语文
c 数据库
c 物理求既选了数据库又选了物理的人结果
name course
c 语文
c 数据库
c 物理
解决方案 »
- dbo.testtest函数return调用dbo.source函数(变量表函数)时语法错误为对象名:dbo.source无效
- 问一个与选择有关的交换律问题
- 如何实现这样的功能?
- 求个简单的处理 ~................................................................................................................
- 这个查询语句怎么写,查出价格最低的(keyword包含了空格)
- 如何将一个查询结果作为基础继续查询
- Sql语句求解检索方案--ACM-ICPC--Standing的更新
- sql server服务 不能启动
- 留名,有分送!!!!!!!
- 这种SQL如何写????能不能写?
- 如何抽取某字段值最大的记录
- sql里的字段类型
where name in ( select name from tb
where course in ('数据库','物理')
group by name
having count(1)=2
)
declare @t table(name varchar(10), course nvarchar(10))
insert @t select 'a' ,N'语文'
insert @t select 'a' ,N'数学'
insert @t select 'a' ,N'化学'
insert @t select 'b' ,N'语文'
insert @t select 'b' ,N'英语'
insert @t select 'b' , N'数据库'
insert @t select 'c' , N'语文'
insert @t select 'c' , N'数据库'
insert @t select 'c', N'物理'
SELECT * FROM @T T WHERE (SELECT COUNT(*)FROM @T WHERE NAME=T.NAME AND COURSE IN(N'数据库',N'物理'))=2
/*name course
---------- ----------
c 语文
c 数据库
c 物理*/
declare @table table (name varchar(1),course varchar(6))
insert into @Table
select 'a','语文' union all
select 'a','数学' union all
select 'a','化学' union all
select 'b','语文' union all
select 'b','英语' union all
select 'b','数据库' union all
select 'c','语文' union all
select 'c','数据库' union all
select 'c','物理'select c.* from @table c
right join (
select a.name from
(select * from @table where course='数据库') a join (select * from @table where course='物理') b
on a.name=b.name) d
on c.name=d.name/*
name course
---- ------
c 语文
c 数据库
c 物理
*/
where a.course = '数据库' and b.course = '物理' and a.name = b.name
(
[name] varchar(10),
course varchar(20)
)
insert into #TT select 'a','语文'
insert into #TT select 'a','数学'
insert into #TT select 'a','化学'
insert into #TT select 'b','语文'
insert into #TT select 'b','英语'
insert into #TT select 'b','数据库'
insert into #TT select 'c','语文'
insert into #TT select 'c','数据库'
insert into #TT select 'c','物理'select T.* from #TT T join
(
select A.[name] from
(
select * from #TT where course='数据库'
) A join
(
select [name] from #TT where course='物理'
) B on A.[name]=B.[name]
) AB on T.[name]=AB.[name]name course
---------- --------------------
c 语文
c 数据库
c 物理(3 行受影响)