学生表A ID name
1 AA
2 BB
课程表B ID cname soccer
1 语文 80
1 数学 90
2 语文 89
2 英语 70查询 即选择了 语文又选择了数学的学生信息,我这样写的
select * from A
where id in(select t1.id from (select * from B
where cname='语文') t1,(select * from B
where cname='数学')t2where t1.id=t2.id)
不知道有没有简单的方法???
1 AA
2 BB
课程表B ID cname soccer
1 语文 80
1 数学 90
2 语文 89
2 英语 70查询 即选择了 语文又选择了数学的学生信息,我这样写的
select * from A
where id in(select t1.id from (select * from B
where cname='语文') t1,(select * from B
where cname='数学')t2where t1.id=t2.id)
不知道有没有简单的方法???
解决方案 »
- sql2000的字符转换
- 请教高手一个存储过程的问题
- 我的数据库达到15G,主要在一个数据表中,包含有图片,现在不能增加记录了,怎么解决?
- 在win2003上安装了SQL Server2000,为什么在其他机器上的SQL Server2000无法连接到2003上的SQL Server2000呢?提示服务器不存在或者连接超
- 一条select语句取一个值,请指点一下
- 在 qa里 查询不是当然数据库的数据表.
- SQL SERVER用户登录的问题
- 关于SQL变量的问题
- 请教Sql Server 存储过程相关问题:传入参数是否支持类似不定元素的数组
- 请高手指教,数据恢复与重新如库的软件开发,用SQL和DELPHI做.
- 网页编程中怎样控制SQL语句的执行数量
- 简单的SQL 连接查询 疑问
from A
where (select count(distinct cname) from B where id=a.id and cname in('语文','数学'))=2
if object_id('[A]') is not null drop table [A]
go
create table [A]([ID] int,[name] varchar(2))
insert [A]
select 1,'AA' union all
select 2,'BB'
if object_id('[B]') is not null drop table [B]
go
create table [B]([ID] int,[cname] varchar(4),[soccer] int)
insert [B]
select 1,'语文',80 union all
select 1,'数学',90 union all
select 2,'语文',89 union all
select 2,'英语',70
---查询---
select *
from A
where (select count(distinct cname) from B where id=a.id and cname in('语文','数学'))=2---结果---
ID name
----------- ----
1 AA(所影响的行数为 1 行)
select * from A where ID in
(
select id from B where cname='数学' and
id in(select id from B where cname='语文')
)
没有想到什么好方法,友情up
insert [A]
select 1,'AA' union all
select 2,'BB'
create table [B]([ID] int,[cname] varchar(4),[soccer] int)
insert [B]
select 1,'语文',80 union all
select 1,'数学',90 union all
select 2,'语文',89 union all
select 2,'英语',70select id from
(
select distinct id from b where cname = '语文'
union all
select distinct id from b where cname = '数学'
) t
group by id having count(1) = 2
/*
id
-----------
1(所影响的行数为 1 行)
*/
select a.* from a where id in
(
select id from
(
select distinct id from b where cname = '语文'
union all
select distinct id from b where cname = '数学'
) t
group by id having count(1) = 2
)
/*ID name
----------- ----
1 AA(所影响的行数为 1 行)*/drop table a , b
select * from A where id in
(
select id from B b1 where cname='语文' and exists (select * from B where id=b1.id and cname='数学')
)select A.id,a.name from A join (select id from B b1 where cname='语文' and exists (select * from B where id=b1.id and cname='数学')) temp1 on a.id=temp1.id
insert @t1 select 1,'AA'
union all select 2,'BB'
declare @t2 table(cid int, cname nvarchar(8), cstore int)
insert @t2 select 1, N'数学', 80
union all select 1, N'语文', 90
union all select 2, N'数学', 79
union all select 2, N'英语', 80select
sname
from
@t1
where not exists
(select * from @t2 where cid=sid and cname not in (N'语文', N'数学'))