Could u please help me on this my sql query,

select STUD_ID,unit_c 
from FCHE_grad 
where (unit_c in("C0001","C0002","ENG300","K0001","K0002")) 
order by STUD_ID

the above query returns student_id, who have 4 unit . I want to display the student id, who having all the above 5 units?

有帮助吗?

解决方案

Use a having cluase to check if the student is in all 5 units

select * from FCHE_grad
where stud_id in (select STUD_ID
from FCHE_grad 
where (unit_c in('C0001','C0002','ENG300','K0001','K0002')) 
group by STUD_ID
having count(stud_id)=5);

Fiddle

其他提示

you should ' ' for varchars in where clause

try this

SELECT STUD_ID,unit_c 
FROM FCHE_grad 
WHERE unit_c ='C0001' AND 'C0002' AND 'ENG300' AND 'K0001' AND 'K0002'
ORDER BY STUD_ID
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top