这是我得到的数据:
类表
部门编号标题
资讯200 XXXXXX
资讯343 111111
信息448 AAAAAA
教表
用户名部号
信息343
B信息343
信息200
B信息200
信息448
C信息200
C信息343
问题是:A和B都教什么课程?
我想的是做一些像SELECT * FROM Class WHERE number = (...)
这样的事情。
其结果应该是:
部门编号标题
资讯343 111111
我怎么写正确的代码,请帮助!谢谢。
发布于 2018-10-15 04:18:02
一种方法是
select c.dept, c.number, c.title
from class c
join teaches t on t.dept = c.dept
and t.number = c.number
where t.username in ('A','B')
group by c.dept, c.number, c.title
having count(distinct t.username) = 2
这将按类对数据进行分组,然后只考虑有关用户的记录。一个组需要同时拥有两个用户。
https://stackoverflow.com/questions/52809507
复制相似问题