我有两张桌子
BatchDetails
BatchCode MaxStd
------------------
B001 12
B002 14
B003 10
AdmissionBatch
Batch Rollno
---------------
B001 1
B001 2
B002 3
B003 4
B003 5
我需要这些批次的BatchCode
没有最大学生。我写了一个查询,但它无法正常工作
select
batchCode, MaxStd, count(rollno)
from
BatchDetails as a
join
AdmissionBatch as b on a.batchcode = b.batch
group by
batchcode, maxStd
having
count(rollno) < maxSTD
此查询无效,因为如果特定批次中没有学生,则批次将不会显示
我也试过了子查询,但没有帮助
请帮忙
答案 0 :(得分:1)
使用左连接而不是内连接。
select batchCode,MaxStd, count(rollno) from BatchDetails as a
LEFT JOIN AdmissionBatch as b on a.batchcode=b.batch
group by batchcode,maxStd
having count(rollno)< maxSTD
答案 1 :(得分:1)
即使没有匹配项,您也需要左联接才能保留结果:
select batchCode,MaxStd, count(rollno)
from BatchDetails as a
left outer join AdmissionBatch as b
on a.batchcode=b.batch
group by batchcode,maxStd
having count(rollno)< maxSTD