同时选择语文和数学的学生的姓名,需要去重表:
姓名 课程
张三 语文
李四 数学
王二 语文
李四 语文
张三 数学
张三 英语选择结果:
张三
李四
姓名 课程
张三 语文
李四 数学
王二 语文
李四 语文
张三 数学
张三 英语选择结果:
张三
李四
解决方案 »
- 一个关于日期的SQL语句
- SQL插入时间数据
- 有没有控件可以实现生成行号的功能?
- 网站数据库无法以连接
- 如何将结果值中的某一字段数据group by以后合并到一个字段
- 有請馬可兄!!:關於同步的問題
- 规整的word文档,怎么将其按字段导入数据库中,如ACCess?
- ▲▲ORACALE中ROWID的用法请举例!▲▲
- 我在一个存储过程中把查询到的数据写到一个表中,然后再从表中返回数据,在数据库中运行正常,但在用ASP调用时会出错,而直接返回数据,不写到
- 再再送分,来者10
- 请问一个SQL语句统计对应多个记录,只选择其中一条记录的办法
- 急急急急!SQL2000数据库表的内容被误删.请问怎么能恢复!记录格式是新建格式..不是覆盖。。 急急急 有尝求人解决!QQ:727979739 送一年QQ会员
from 表
where 课程 in('语文','数学')
group by 姓名
having count(distinct 课程)=2
declare @t table (姓名 varchar(4),课程 varchar(4))
insert into @t
select '张三','语文' union all
select '李四','数学' union all
select '王二','语文' union all
select '李四','语文' union all
select '张三','数学' union all
select '张三','英语'select 姓名 from
(
select * from @t where 课程='语文'
union all
select * from @t where 课程='数学'
) a group by 姓名 having(count(1)>1)
/*
姓名
----
李四
张三
*/
(
Name nvarchar(50),
KeC nvarchar(50)
)insert into @tab(Name,Kec)
select '张三', '语文'
union all
select '李四', '数学'
union all
select '王二' ,'语文'
union all
select '李四', '语文'
union all
select '张三', '数学'
union all
select '张三' ,'英语'select * from @tab where Name in(select Name from @tab where Kec='语文')
and keC='数学'
select distinct Name from @tab where Name in(select Name from @tab where Kec='语文')
and keC='数学'
select distinct Name from A where exists(select A where 课程 =语文)and 课程 =数学
where exists
(
select 1 from sb bb
where aa.课程='语文' and bb.课程='数学' and aa.姓名=bb.姓名
)
--姓名 课程
--张三 语文
--李四 数学
--王二 语文
--李四 语文
--张三 数学
--张三 英语declare @t table (姓名 varchar(4),课程 varchar(4))
insert into @t
select '张三','语文' union all
select '李四','数学' union all
select '王二','语文' union all
select '李四','语文' union all
select '张三','数学' union all
select '张三','英语'
--选择结果:
--张三
--李四select distinct 姓名 from @t where 课程 in('语文','数学')
group by 姓名
having count(distinct 课程)=2
(6 行受影响)
姓名
----
李四
张三(2 行受影响)
insert into tb
select '张三','语文' union all
select '李四','数学' union all
select '王二','语文' union all
select '李四','语文' union all
select '张三','数学' union all
select '张三','英语'
go
select a.姓名 from tb a left join tb b on a.姓名=b.姓名 where a.课程='语文' and b.课程='数学'
结果为:
姓名
张三
李四
(2 行受影响)