You would normally do this using exists
:
select t1.*,
(case when exists (select 1
from table2 t2
where t2.student = t1.student and t2.subject = 'math'
)
then 'yes' else 'no'
end) as has_math
from table1 t1;
Unlike Tim's answer, this is guaranteed to return only one row per student, even if there are multiple 'math'
rows in the second table.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…