求一SQL语句

zx005 2016-03-13 09:51:11
表 a
studentid name
1 张三
2 李四
3 王五

表b
id studentid subject score
1 1 语文 90
2 1 数学 100
3 1 英语 95
4 2 语文 100
5 2 数学 100
6 2 英语 100
6 3 语文 89
7 3 数学 95
8 3 英语 100
求一SQL语句 返回全是100分的a表记录(b表外键studentid关联)
执行SQL语句后应为
studentid name
2 李四
...全文
121 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zx005 2016-03-14
  • 打赏
  • 举报
回复
谢谢大神!!
唐诗三百首 2016-03-14
  • 打赏
  • 举报
回复

create table 表a
(studentid int,name varchar(10))

insert into 表a
  select 1,'张三' union all
  select 2,'李四' union all
  select 3,'王五' 

create table 表b
(id int,studentid int,subject varchar(10),score int)

insert into 表b
  select 1,1,'语文',90 union all
  select 2,1,'数学',100 union all
  select 3,1,'英语',95 union all
  select 4,2,'语文',100 union all
  select 5,2,'数学',100 union all
  select 6,2,'英语',100 union all
  select 6,3,'语文',89 union all
  select 7,3,'数学',95 union all
  select 8,3,'英语',100


select a.*
 from 表a a
 inner join
 (select studentid 
   from 表b
   group by studentid
   having sum(score)=count(1)*100) b on a.studentid=b.studentid

/*
studentid   name
----------- ----------
2           李四

(1 row(s) affected)
*/
顾西昂 2016-03-14
  • 打赏
  • 举报
回复
建议学下exists,效率高。 我不太会,就写个not in 吧

select * from 表a where studentid not in
(select studentid  from 表b  score<>100)
xxfvba 2016-03-14
  • 打赏
  • 举报
回复
select * from a where not exists (select * from b where a.studentid =b.studentid and score<>100)

34,591

社区成员

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

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