我有这张桌子
name | prof |grade
------------------------
yossi math 100
tami math 70
yossi phisic 100
tami phisic 100
oren math 100
oren phisic 80
dor history 70
Run Code Online (Sandbox Code Playgroud)
查询应返回数学和phisic等级为100的学生姓名正确的unswer是yossi我使用了以下内容
SELECT name FROM [dbo].[Class_grade]
where prof in ('math', 'phisic') and grade = 100
Run Code Online (Sandbox Code Playgroud)
但它返回更多的名字为什么?什么是正确的查询?谢谢
select name
from Class_Grade
where grade = 100 and
prof in ('math', 'phisic')
group by name
having count(distinct prof) = 2
Run Code Online (Sandbox Code Playgroud)
使用分组依据name并过滤掉行having.确保计算不同的出现次数prof.在这种情况下它是2,因为你的in子句中有2个值.
| 归档时间: |
|
| 查看次数: |
90 次 |
| 最近记录: |